2013-03-23 84 views
1

現在我們所面臨的問題在調用對DataService(解析)控制器調用,問題是,這個代碼不工作:

function MainCtrl($scope, DataService, $location) 
{ 

    $scope.actionList = function() { 

      // Call the service and fetch the list of signatures that match the given action ID 
      DataService.getActions(function (results) { 
       $scope.$apply(function() { 
        // Apply the results to the signatureList model so it will refresh the table in the view 
        $scope.actionList = results; 
       }); 
      }); 

    }; 
} 

我把一個斷點DataService的線,但它並沒有撞到,但如果我用這種方式實現按Ctrl它得到調用和它的作品!:

function MainCtrl($scope, DataService, $location) 
{    

       // Call the service and fetch the list of signatures that match the given action ID 
       DataService.getActions(function (results) { 
        $scope.$apply(function() { 
         // Apply the results to the signatureList model so it will refresh the table in the view 
         $scope.actionList = results; 
        }); 
       });   

    } 

任何想法,爲什麼會這樣?

除此之外,一旦在工作(與第二實現)我想顯示活動的屬性,如果我試圖讓這樣的屬性它不工作:

<div id="wrapper"><div id="scroller"> 
<div ng-controller="MainCtrl"> 
    <ul id="thelist"> 
     <li ng-repeat="action in actionList">{{action.get('Action')}}</li> 
    </ul> 
</div> 
</div></div> 

但如果我嘗試獲取像{{action}}這樣的整個對象,我可以看到所有條目。

+0

任何錯誤在控制檯 – 2013-03-23 11:21:18

+0

我看到的第一個問題是兩種功能:和數組被命名爲因爲'DataList'是角應用程序的一部分時'actionList' – 2013-03-23 11:26:13

+1

不應該使用'$ .apply'。標記中的{{action.get()}}'沒有意義。爲什麼從'DataService'返回的數組中的對象在其中具有函數...或者它們是做什麼的?在ng-repeat標記中只需要{{Action}}。顯示'DataService'模塊或工廠的代碼 – charlietfl 2013-03-23 13:53:50

回答

5

控制器更改爲

function MainCtrl($scope, DataService, $location) { 

    $scope.getActionList = function() { 
     DataService.getActions(function(results) { 
        $scope.$apply(function() { 
           $scope.actionList = results; 
          }); 
       }); 

    }; 

    $scope.getActionList(); 
} 

然後,如果你想重裝actionList呼叫getActionList()

0

任何想法,爲什麼會這樣?

在你的第一個例子,你只是一個定義上的對象$scope命名actionList功能。定義一個函數將不會執行它(參見@ Arun的答案)。

要在視圖中顯示數據,可以使用普通的JavaScript點表示法。如果action物體看起來是這樣的:

{ prop1: 'property 1', prop2: [1, 2], prop3: { name: 'Javito'} } 

如下你可以寫視圖/ HTML:

<ul id="thelist"> 
    <li ng-repeat="action in actionList"> 
     prop1: {{action.prop1}}<br> 
     prop2: {{action.prop2[0]}} {{ action.prop2[1] }}<br> <!-- or use another ng-repeat here --> 
     prop3.name: {{action.prop3.name}} 
    </li> 
</ul>