2015-10-08 62 views
0

我有一個頁面如下自調用函數定義工作。控制器不工作。有人可以建議我最新怎麼了?Angularjs控制器不掛接到一個模塊

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="demoApp"> 
<head> 
    <title>AnjularJs Hello World</title> 
</head> 
<body data-ng-controller="CustomerController"> 
    <h2>Customers</h2> 
    <div id="Trial">{{name}}</div> 
    <script src="Scripts/angular.js"></script> 
    <script> 
     (function() { 
      alert("Inside Self invoking function. This is popping up."); 
      CustomerController = function ($scope) { 
       alert("Inside controller. But this is NOT popping up"); 
       $scope.name = 'HeHe'; 
      }; 
      CustomerController.$inject = ['$scope']; 
      angular.module('demoApp').controller('CustomerController', CustomerController); 
     }()); 
    </script> 
</body> 
</html> 

控制器內部的警報沒有彈出。第一個警報彈出。另外,id =「Trial」的div不會打印名稱。

我錯過了什麼?

回答

2

你永遠不會創建demoApp - 模。隨着angular.module(name)得到已創建的模塊,與angular.module(name, dependencies)設置(創建)的模塊。因此,簡單地添加:

angular.module('demoApp', []); 

線以上angular.module('demoApp')和你的繩索工作得很好:

(function() { 
 
    alert("Inside Self invoking function. This is popping up."); 
 

 
    CustomerController = function ($scope) { 
 
       alert("Inside controller. But this is NOT popping up"); 
 
       $scope.name = 'HeHe'; 
 
      }; 
 
    CustomerController.$inject = ['$scope']; 
 
    
 
    angular.module('demoApp', []); 
 
      
 
    angular.module('demoApp').controller('CustomerController', CustomerController); 
 
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body ng-app="demoApp" data-ng-controller="CustomerController"> 
 
    <h2>Customers</h2> 
 
    <div id="Trial">{{name}}</div> 
 
</body>

+0

謝謝你,我知道了。它其實是一個微不足道的錯誤。我會在幾分鐘內接受答案。 – VivekDev

+1

Kk。很高興我能幫上忙。 – LionC

相關問題