我正在尝试定义两个角度模块myApp1
& myApp2
。
来自myApp1
我正在使用myApp2
的服务。
以下是我在JSfiddle
HTML
<div ng-app="myApp1" ng-controller="mycontroller">
{{saycheese}}
</div>
JS
var myApp2 = angular.module('myApp2', []);
var myApp1 = angular.module('myApp1', []);
myApp1.service('myservice', function() {
this.sayHello = function(test) {
return "from service" + test;
};
this.sayHello1 = function() {
return "from service - sayHello1"
};
});
myApp2.service('myservice2', function() {
this.sayCheese = function(test) {
return "from service of myApp2" + test;
};
});
myApp1.factory('myfactory', function() {
return {
sayHello: function() {
return "from factory!"
}
};
});
//defining a controller over here
myApp1.controller("mycontroller", ["$scope", "myfactory", "myservice", "myservice2", function($scope, myfactory, myservice, myservice2) {
$scope.saycheese = [
myfactory.sayHello(),
myservice.sayHello("abcd"),
myservice.sayHello1(),
myservice2.sayCheese("abcdefghij!")
];
}]);
但是当我检查 CONSOLE LOGS 时,angular会抱怨no module: myApp
。
JSfiddle在这里http://jsfiddle.net/PxdSP/3050/
有人可以帮我这个吗?
答案 0 :(得分:1)
myApp1 -> myApp & myApp2 -> myApptwo
。myApptwo
注入myApp
,否则您将无法访问myApptwo
var myApptwo = angular.module('myApptwo', []);
var myApp = angular.module('myApp', ['myApptwo']);
myApp.service('myservice', function() {
this.sayHello = function(test) {
return "from service" + test;
};
this.sayHello1 = function() {
return "from service - sayHello1"
};
});
myApptwo.service('myservice2', function() {
this.sayCheese = function(test) {
return "from service of myApp2" + test;
};
});
myApp.factory('myfactory', function() {
return {
sayHello: function() {
return "from factory!"
}
};
});
//defining a controller over here
myApp.controller("mycontroller", ["$scope", "myfactory", "myservice", "myservice2", function($scope, myfactory, myservice, myservice2) {
$scope.saycheese = [
myfactory.sayHello(),
myservice.sayHello("abcd"),
myservice.sayHello1(),
myservice2.sayCheese("abcdefghij!")
];
}]);
&#13;
<div ng-app="myApp" ng-controller="mycontroller">
{{saycheese}}
</div>
&#13;