簡體   English   中英

AngularJS視圖不顯示工廠返回的數據

[英]Angularjs view not displaying data returned from factory

我設置了一個服務,該服務使用了返回json的rails api。 根據控制台輸出,該請求將返回:

Resource {tests: Array[4], $promise: Promise, $resolved: true}

我遇到的問題是我認為沒有任何顯示。 我是否需要以某種方式轉換返回的結果? 當我通過郵遞員ping該路由時,我得到一個json對象作為我的結果集。

我有以下設置:

/service/test.js

  angular.module('myApp')
      .factory('Test', function($resource) {
        return $resource('http://api.myapp-dev.com:3000/tests/:id', { id: '@_id' },
        {
          'index': {
            method:'GET',
            headers: {
              'Authorization':'<a token>'
            },
            isArray: false
          }
    });
  });

/controllers/tests.js

angular.module('myApp')
  .controller('TestsCtrl', function ($scope, Test) {
    $scope.tests = Test.index();
  });

views / tests.html

<div>
  <ul ng-repeat="test in tests">
    <li>{{ test.value }}</li>
  </ul>
</div>

您應該在控制器中執行以下操作。

Test.index(
  function(response) {
    $scope.tests = response.tests;
  },
  function(error) {
  }
)

也有其他方式可以做到這一點。

Test.index().$promise.then(
  function(response) {
    $scope.tests = response.tests;
  },
  errorCallback
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM