2016-08-06 89 views
0
(function() { 
    'use strict'; 

    function myController($scope, $http) { 
     var vm = this; 
     $scope.text = "Delhi"; 
    } 

    myController.$inject = ["$scope", "$http"]; 
    angular 
     .module("app") 
     .controller("myController", myController); 
})(); 

和我的HTML代码这里简单扶养注射不工作

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <script src="angular.js"></script> 
    <script src="app.js"></script> 
</head> 
<body ng-app="app"> 
    <div ng-controller="myController"> 
     <p>I am from {{text}}</p> 
    </div> 
</body> 
</html> 

但我预期我的项目不能正常工作。给出以下错误

https://www.dropbox.com/s/itd5ryar56zqcxn/Screenshot%202016-08-06%2023.46.04.png?dl=0

angular.js:68 Uncaught Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 
http://errors.angularjs.org/1.5.5/$injector/nomod?p0=app 
+0

是'app'模块在另一个文件中声明? – developer033

+0

不,这就是全部。请帮助解决。 –

+0

所以你必须像这样声明你的'app':'angular.module(“app”,[]);' – developer033

回答

0

没有与代码的几个问题,

(i)您需要首先定义模块,然后注入控制器

(ii)您忘了添加空模块'[]'

(function() { 
    "use strict"; 
    angular 
     .module("app",[]) 
     .controller("myController", myController); 
    function myController($scope, $http) { 
     var vm = this; 
     $scope.text = "Delhi"; 
    } 
    myController.$inject = ["$scope", "$http"];  
}()); 

这里是工作App

+0

我知道它可以正常工作,但我也看到很多控制器在没有[]的情况下工作。 https://www.dropbox.com/s/bed9nwy5iacdvws/Screenshot%202016-08-07%2000.31.25.png?dl=0 –

+0

@GopalSharma是可能的,但是当你声明一个模块时,你需要定义依赖关系,甚至在你的例子中它应该已经被声明在某个地方。阅读更多http://stackoverflow.com/questions/19957280/angularjs-best-practices-for-module-declaration – Sajeetharan