2016-08-06 2 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> 

ですが、予想通り、私は私のプロジェクトが機能していません。あなたが最初にモジュールを定義する必要があり、その後、あなたは(ⅱ)コントローラに

を注入コードでいくつかの問題、

(i)があり、エラーの下

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

を与えます空の依存関係をモジュールに追加するのを忘れた'[]'

(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