2015-04-17 26 views
0

我正在忙于接受一个教程,作为一名角度新手,并且已经达到引入Controller的地步。我的MVC视图生成以下标记:Angular为什么看不到我的控制器?

<script> 
    function PersonController($scope) { 
     $scope.persons = [ 
      { firstName: 'Brady', lastName: 'Kelly' }, 
      { firstName: 'James', lastName: 'Brown' }, 
      { firstName: 'Charles', lastName: 'Manson' } 
     ]; 
    } 
</script> 
<table class="table" data-ng-controller="PersonController"> 
    <tr> 
     <th> 
      <span>Last Name</span> 
     </th> 
     <th> 
      <span>First Name</span> 
     </th> 
     <th></th> 
    </tr> 

    <tr data-ng-repeat="pers in persons | orderBy:'lastName'"> 
     <td> 
      {{pers.lastName}} 
     </td> 
     <td> 
      {{pers.firstName}} 
     </td> 
     <td></td> 
    </tr> 
</table> 

当我用ng-init指令来实例化人阵“内联”,如:工作

<table class="table" data-ng-init="persons=[....]"> 

该表中的数据绑定,但是当我用的是ng-controller指令,如我上面的例子,我在Chrome的控制台得到一个错误,指出:

Error: [ng:areq] Argument 'PersonController' is not a function, got undefined

+0

尝试添加NG-应用 – ABOS

+0

你能不能显示完整的代码 – Kushal

+0

同样的错误,在这里http://stackoverflow.com/questions/24929296/error-ngareq-argument-myctrl-is-not-a-function -got-undefined –

回答

2

看起来你需要定义您的合作以不同的方式。我没有看到你的榜样的app定义,所以要确保你有东西,看起来像以下内容,然后链中的控制器关闭app

<html ng-app="app"> 

var app = angular.module('app', 
    [ 
    ]); 

app.controller('PersonController', ['$scope', function ($scope) { 
    $scope.persons = [ 
     { firstName: 'Brady', lastName: 'Kelly' }, 
     { firstName: 'James', lastName: 'Brown' }, 
     { firstName: 'Charles', lastName: 'Manson' } 
    ]; 
}]); 
+0

ng-app必须在那里,看看他得到的错误。 –

+0

@Werlang你是对的,我想同样的tbh。可能最好把它从回答中拿出来更准确地针对问题 – scniro

+0

相当正确,@Werlang,我确实有一个''。我敢肯定,如果缺少,我的初始数据绑定,没有控制器,将无法工作。 – ProfK

0

您需要定义你的角的js模块并在PersonController中注入$ scope。

<script> 
     angular.module('app', []); 
     function PersonController($scope) { 
      $scope.persons = [ 
       { firstName: 'Brady', lastName: 'Kelly' }, 
       { firstName: 'James', lastName: 'Brown' }, 
       { firstName: 'Charles', lastName: 'Manson' } 
      ]; 
     } 
     PersonController.$inject = ['$scope']; 
    </script> 
相关问题