【问题标题】:Angular $http using json file works but using json file with $httpBackend is not working to display data使用 json 文件的 Angular $http 有效,但使用带有 $httpBackend 的 json 文件无法显示数据
【发布时间】:2016-09-02 21:48:18
【问题描述】:

我可以伪造使用 $http 但使用 $resource/ $httpBackendjson 文件中获取数据我没有得到任何结果

$http.get(有效)

 $http.get('api/devices.json')
      .then(function (result) {

      ...//

上面的工作正常,但 $httpBackend 将与 json inline 一起工作,但不指向 JSON 文件

调用 deviceResourceMock 模块的控制器文件

deviceResource.query(function(data) {    
       vm.devices = data; 
    });

deviceResourceMock 模块

app.run(function ($httpBackend) {

    var devices = 'test.json';  // Put new json file in same directory

    // ABOVE DOES NOT WORK 

这确实在下面工作

var devices = {"Devices":  [
        {
            "DeviceId": 1,
            "DeviceStatus": "Leaf Rake",
            "id": "GDN-0011",
            "ha": "Leaf rake with 48-inch wooden handle.",
            "Urt": "blah"
        }
]};

URL 和何时获取

var deviceUrl = "/api/devices";
$httpBackend.whenGET(deviceUrl).respond(devices.Devices);

思考为什么它不起作用?

【问题讨论】:

    标签: javascript angularjs json angularjs-directive angularjs-scope


    【解决方案1】:

    是“devices.Devices”的功能吗?

    我觉得代码应该是这样的

    $httpBackend.whenGET(deviceUrl).respond(function(method,url,data) {

    return devices.Devices;
    

    });

    【讨论】:

    • {"Devices": 是数据将如何包装在该对象中......它是Web api如何发送数据,因此需要使用我显示的代码执行devices.Devices,它是有效的 JSON 来包装它不是一个函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    相关资源
    最近更新 更多