1

這已被問過,但它沒有回答我的問題。我對角度相當陌生,現在我只是把事情放在一起。我試圖讓我的工廠在我的控制器內工作。不過,我不斷收到以下錯誤在我的控制檯:參數'indexController'是不是一個函數,得到undefined

Argument 'indexController' is not a function, got undefined 

我打破了在不同的目錄服務和控制器和我已經加入了服務和控制器<script>到index.html文件。

我使用IntelliJ並使用其插件創建樣板。

<body > 
    <ul class="menu"> 
    <li><a href="#/view1">view1</a></li> 
    <li><a href="#/view2">view2</a></li> 
    </ul> 


    <!--[if lt IE 7]> 
     <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> 
    <![endif]--> 

    <div> 

      <p>Name: <input type="text" ng-model="name"> </p> 
      <h1>Hello {{name}}</h1> //this works just fine 

    </div> 

    <div ng-controller="indexController"> //doesn't appear 
     <ul> 
      <li ng-repeat="x in nameArray"> 
       {{x.name + ' is : '+x.age+' years old'}} 
      </li> 
     </ul> 

    </div> 

控制器:

angular.module('myApp',[]) 
    .controller('indexController', ['$scope', 'Contact', function($scope, Contact){ 

     var names = [ 
      {name:'Drew' , age: 30}, 
      {name:'Meike', age: 32}, 
      {name:'Garry', age:64} 
     ]; 

     Contact.add(names); 

    $scope.nameArray = Contact.get(); 



}]); 

FactoryService:

angular.module('myApp',[]) 
    .factory('Contact', function ContactFactory(){ 
     var personNames = []; 
     return{ 

      add: function(name) { 
       personNames.add(name); 
      }, 

      get: function(){ 
       return personNames; 
      } 

     } 

    }); 

回答

2

你不應該內部FactoryService再次重現角模塊myApp,當你再次創造myApp模塊就衝出舊的註冊組件到那個模塊。所以當你註冊FactoryService它將刪除舊的寄存器組件,在這裏它刪除了indexController控制器。當ng-controller指令獲取評估它搜索indexController控制器模塊內部並拋出一個錯誤

應該

angular.module('myApp') 
    .factory('Contact', function ContactFactory(){ 

,而不是

angular.module('myApp',[]) 
    .factory('Contact', function ContactFactory(){ 
相關問題