2017-04-08 107 views
0

You can view the error here

我不知道是什麼錯誤,因爲我很新的angularjs。我正在嘗試使用以下代碼來使用webapi服務。

的app.js文件:

(function() { 
    "use strict"; 

    var app = angular.module("productManagement", 
          ["common.services"]); 

}()); 

productListCtrl.js

(function() { 
    "use strict"; 
    angular 
     .module("productManagement") 
     .controller("ProductListCtrl", 
        ["productResource" ,ProductListCtrl]); 

    function ProductListCtrl(productResource) { 
     var vm = this; 

     productResource.query(function (data) { 
      vm.products = data; 
     }); 
    } 
}()); 

commonservices.js

(function() { 
    "use strict"; 

    angular.module("common.services", ["ngResource"]) 
    .constant("appSettings", { 
     serverPath: "http://localhost:55755/" 
    }); 
}()); 

productResource.js

(function() { 
    "use strict"; 

    angular.module("common.services"). 
    factory("productResource", ["$resource", 
     "appSettings", 
     productResource]) 

    function productResource($resource, appSettings) { 
     return $resource(appSettings.serverPath + "/api/products/:id"); 
    } 
}); 

的index.html

<!DOCTYPE html> 
<html> 
<head lang="en"> 
    <meta charset="UTF-8"> 
    <title>Acme Product Management</title> 

    <!-- Style sheets --> 
    <link href="Content/bootstrap.css" rel="stylesheet" /> 
</head> 

<body ng-app="productManagement"> 
    <nav class="navbar navbar-default"> 
     <div class="container-fluid"> 
      <div class="navbar-header"> 
       <div class="navbar-brand">Acme Product Management</div> 
      </div> 
     </div> 
    </nav> 

    <div class="container"> 
     <div ng-include="'app/products/productListView.html'"></div> 
    </div> 

    <!-- Library Scripts --> 
    <script src="scripts/angular.js"></script> 
    <script src="Scripts/angular-resource.js"></script> 
    <!-- Application Script --> 
    <script src="app/app.js"></script> 

    <!-- Services --> 
    <script src="Common/common.services.js"></script> 
    <script src="Common/productResource.js"></script> 
    <!-- Product Controllers --> 
    <script src="app/products/productListCtrl.js"></script> 
</body> 

</html> 
+0

你是否在html文件中包含了所有文件,看起來像是你在工程文件中的錯誤 –

+0

你能顯示這些腳本加載到HTML文件中的順序嗎? – Claies

+0

我已經添加了index.html文件請看看它 –

回答

-1

糾正你的JS文件的順序。

<script src="app/products/productListCtrl.js"></script> 
<script src="Common/productResource.js"></script> 
<script src="Common/common.services.js"></script> 
<script src="app/app.js"></script> 
0

的問題是,你不執行已爲productResource.js寫的匿名功能塊,這是防止productResourceProvider被註冊:

更改此:

productResource.js

(function() { 
    "use strict"; 

    angular.module("common.services"). 
    factory("productResource", ["$resource", 
     "appSettings", 
     productResource]) 

    function productResource($resource, appSettings) { 
     return $resource(appSettings.serverPath + "/api/products/:id"); 
    } 
}); // <-- Problem lies here. 

更改爲:

(function() { 
    "use strict"; 

    angular.module("common.services"). 
    factory("productResource", ["$resource", 
     "appSettings", 
     productResource]) 

    function productResource($resource, appSettings) { 
     return $resource(appSettings.serverPath + "/api/products/:id"); 
    } 
}()); // <-- Execute the function block. 
相關問題