2013-03-23 85 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>