从不同的文件加载角度控制器而不是1

时间:2015-11-24 15:16:00

标签: javascript angularjs

我正在尝试从不同的js加载控制器,但我没有成功,你们可以帮我加载名为StreetListCtrl.js的控制器吗?

UsersListCtrl加载完美只是StreetListCtrl不加载/

我的HTML:

<!DOCTYPE html>
<html ng-app='myApp' lang="en">
  <head>
    <meta charset="utf-8">
    <title>angularjs</title>
    <script src="bower_components/angular/angular.min.js"></script>
    <script src='public/javascripts/ng-controller.js'></script>
  </head>
  <body>
                <table ng-controller="UsersListCtrl">
                    <thead>
                      <tr>
                        <th>Username</th>
                        <th>UserId</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr ng-repeat="user in users | orderBy:'id' ">
                        <td><strong>{{ user.name }}</strong></td>
                        <td>{{ user.id }}</td>
                      </tr>
                    </tbody>
                </table>
                  <p>street list </p>

                 <table ng-controller="StreetListCtrl">
                    <thead>
                      <tr>
                        <th>street name</th>
                        <th>id</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr ng-repeat="data in street | orderBy:'id' ">
                        <td><strong>{{ data.name }}</strong></td>
                        <td>{{ data.id }}</td>
                      </tr>
                    </tbody>
                </table>
  </body>
</html>

StreetListCtrl.js

angular.module('myApp')

myApp.controller('StreetListCtrl',['$scope',function($scope){
        $scope.street =[
        {name:'1 smithfield', id:3},
        {name:'2 river plate', id:1333},
        {name:'3 river plate', id:1}
    ];
}])

NG-controller.js:

angular.module('myApp', [])
.controller('UsersListCtrl',['$scope',function($scope){
        $scope.users =[
        {name:'douglas', id:3},
        {name:'bruno', id:1}
    ];
}])

2 个答案:

答案 0 :(得分:1)

你忘记了第二个javascript文件。你有:

<head>
  <meta charset="utf-8">
  <title>angularjs</title>
  <script src="bower_components/angular/angular.min.js"></script>
  <script src='public/javascripts/ng-controller.js'></script>
</head>

应该是:

<head>
  <meta charset="utf-8">
  <title>angularjs</title>
  <script src="bower_components/angular/angular.min.js"></script>
  <script src='public/javascripts/ng-controller.js'></script>
  <script src='public/javascripts/StreetListCtrl.js'></script>
</head>

和StreetListCtrl.js应为:

angular.module('myApp').controller('StreetListCtrl',['$scope',function($scope){
        $scope.street =[
        {name:'1 smithfield', id:3},
        {name:'2 river plate', id:1333},
        {name:'3 river plate', id:1}
    ];
}])

答案 1 :(得分:0)

您遗失<script src='public/javascripts/StreetListCtrl.js'></script> 来自index.html