2017-02-20 59 views
0

我收到此錯誤 - 錯誤:$控制器:ctrlreg具有此名稱的控制器未註冊。我所有的腳本都被加載,並且我將它們全部添加到index.html中。未在XAMPP Web服務上註冊的角度控制器

All scripts are loaded

我的控制器:

(function() { 
    angular.module("nsoftModule").controller("nsoftController", ["nsoftService", function(nsoftService) { 
     var self = this; 
     self.service = nsoftService; 
    }]) 
})(); 

我的指令:

(function() { 
    var weatherMod = angular.module("nsoftModule", []); 
    weatherMod.directive("nsoftDirective", function() { 
     return { 
      templateUrl: "Template/weatherTemplate.html", 
      controller: "nsoftController as nsoftCtrl" 
     } 
    }); 
})(); 

我的服務:

(function() { 
    var weatherMod = angular.module("nsoftModule", []); 
    weatherMod.service("nsoftService", ["http", function(http) { 
     var service = this; 
    }]); 
})(); 

我的模塊:

(function() { 
    var weatherMod = angular.module("nsoftModule", []); 
})(); 

我的模板:

<div class="container"> 
    <div ng-controller="nsoftController"> 
     <p>Name : <input type="text" ng-model="name"></p> 
     <h1>Hello {{name}}</h1> 
    </div> 
</div> 

我app.js:

(function() { 
    angular.module("nsoftApp", ["nsoftModule"]); 
})(); 

而且我的index.html:

​​

回答

0

只是刪除全局變量,因爲你正在使用的匿名自調用函數變量僅限於一個匿名函數。使用像他

(function() { 
    angular.module("nsoftModule").controller("nsoftController", ["nsoftService", function(nsoftService) { 
     var self = this; 
     self.service = nsoftService; 
    }]) 
})(); 

(function() { 
    angular.module("nsoftModule").directive("nsoftDirective", function() { 
     return { 
      templateUrl: "Template/weatherTemplate.html", 
      controller: "nsoftController as nsoftCtrl" 
     } 
    }); 
})(); 

//change http to $http 
(function() { 
    angular.module("nsoftModule").service("nsoftService", ["$http", function($http) { 
     var service = this; 
    }]); 
})(); 

(function() { 
    angular.module("nsoftModule", []); 
})(); 

因爲控制器從指令分配沒有必要宣佈它在html也

<div class="container"> 
<div > 
    <p>Name : <input type="text" ng-model="nsoftCtrl.name"></p> 
    <h1>Hello {{nsoftCtrl.name}}</h1> 
</div> 

(function() { 
    angular.module("nsoftApp", ["nsoftModule"]); 
})(); 
+0

非常感謝您的回答Sachila,但我仍然得到同樣的錯誤甚至是你的改進... –

+0

檢查此。我會更新答案https://plnkr.co/edit/9d64fOb9QhNgCeKl90VY?p=preview –

+0

比你非常,它的作品,真棒! –