Angular抱怨丢失的模块没有在所有的情况下声明

时间:2017-03-07 03:08:58

标签: javascript c# angularjs asp.net-mvc

我正在尝试定义两个角度模块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/

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

  1. 它的命名约定存在问题,您不能在模块名称中使用数字。我将它们重命名为myApp1 -> myApp & myApp2 -> myApptwo
  2. 第二个问题是您未myApptwo注入myApp,否则您将无法访问myApptwo
  3. 的服务

    &#13;
    &#13;
    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;
    &#13;
    &#13;