这是我使用angularjs进行的演示,用于创建服务文件并将服务添加到控制器。
我的演示有两个问题:
- 一种是
<script src="HomeController.js">
在<script src="MyService.js">
我收到此错误之前,
错误:[ng:areq]参数'HomeController'不是一个函数,未定义
- 另一种是,当我把
<script src="MyService.js">
之前<script src="HomeController.js">
我碰到下面的错误,
错误:[$ injector:unpr]未知提供程序:MyServiceProvider <-MyService
我的来源:
档案Index.html
:
<!DOCTYPE html>
<html >
<head lang="en">…</head>
<body ng-app="myApp">
…
<div ng-controller="HomeController">
<div ng-repeat="item in hello">{{item.id + item.name}}</div>
</div>
<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-route.js"></script>
<!-- App libs -->
<script src="app/app.js"></script>
<script src="app/services/MyService.js"></script>
<script src="app/controllers/HomeController.js"></script>
</body>
</html>
档案HomeController.js
:
(function(angular){
'use strict';
var myApp = angular.module('myApp',[]);
myApp.controller('HomeController',function($scope,MyService){
$scope.hello=[];
$scope.hello = MyService.getHello();
});
})(window.angular);
档案MyService.js
:
(function(angular){
'use strict';
var myApp = angular.module('myApp',[]);
myApp.service('MyService', function () {
var hello =[ {id:1,name:'cuong'},
{id:2,name:'nguyen'}];
this.getHello = function(){
return hello;
};
});
})(window.angular);