[英]Load controllers in angular from different files instead just 1
我正在尝试从不同的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}
];
}])
您忘记了第二个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}
];
}])
您缺少index.html中的<script src='public/javascripts/StreetListCtrl.js'></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.