主要模塊app.js
:爲什麼不能省略模塊依賴於angular.module時定義組件
angular.module("myApp",["phoneList"]);
,我按照tutorial使名爲sperate組件模塊phoneList
angular.module("phoneList")
//^ omit the empty bracket here, cause no dependency needed
.component('phoneList', {
templateUrl: 'template/phone-list.html',
controller: function(){
this.phones = [
{name: "iphone", snippet: "designed by apple"},
{name: "nexus", snippet: "phone that could explode"}
]}
});
但它會抱怨:
angular.min.js:6 Uncaught Error: [$injector:nomod] http://errors.angularjs.org/1.6.1/$injector/nomod?p0=phoneList
at angular.min.js:6
at angular.min.js:25
at b (angular.min.js:24)
at Object.module (angular.min.js:25)
at phone-list.js:5
W我添加一個空[]
回:
angular.module("phoneList",[])
.component(//balabla)
事情會工作得很好。我檢查了API Reference,它證明依賴是可選的,因此可以省略。
傳遞一個參數將檢索現有的'angular.Module',而傳遞多個參數會創建一個新的'angular。模塊' 當你正在創建'phoneList'一個新模塊,所以你必須通過第二個參數 – muneebShabbir