2016-05-09 31 views
-1

我在.net中創建了一個RESTful API。html是使用angularJS創建的。AngularJS Post方法獲取錯誤

的問題是,當頁面加載時,我發現了以下錯誤: enter image description here

和「插入」按鈕不起作用了。

這是我的插入腳本和HTML全文可以在鏈接中找到:

$scope.insert = function insert() { 
     var data = {"id": 3, "name": $scope.name, "description":$scope.description , "price": $scope.price, "categoryid": $scope.categoryid}; 
     $http.post(
      'http://localhost:22258/api/product', 
      JSON.stringify(data), 
      { 
       headers: { 
        'Content-Type': 'application/json' 
       } 
      } 
     ).success(function (data) { 
      $scope.products.push(data); 
     }); 

https://jsfiddle.net/0xb2nh9o/

即使你是友善幫我做編輯鍵設置爲以下模型:https://jsfiddle.net/benfosterdev/UWLFJ/使HTTPPut成爲API,這將是美好的。

所有RESTful請求工作,API的工作,與郵遞員

測試我金達新手到AngularJS所以你的幫助將是我非常有用的。非常感謝 !

回答

1

您的插入函數是'ProductController'的一部分,而不是'FormCtrl'。

因此,在你的HTML,這樣做:

<div ng-controller="ProductController"> 

,你會擺脫錯誤的。

0

如果您打開the URL reported in the error trace,您會看到angular正在抱怨「FormCtrl」爲undefined

那是因爲你命名你的控制器ProductController這裏:

app.controller("ProductController", function ($scope, $http) { 

但在這裏需要它作爲FormCtrl

<div ng-controller="FormCtrl"> 

讓那些匹配和錯誤會自行消失。

+0

插入操作現在可以工作,但是在完成插入之後如何實現表格自動更新? – TrulyXax

+0

你有一堆調用'localhost',所以不能從jsfiddle測試 –

+0

我有angular.js:10071TypeError:無法讀取GetTemplate函數 – TrulyXax

0

您在模板中定義了2個控制器,ProductControllerFormCtrl。 由於您沒有formCtrl的控制器定義,它會引發錯誤。

如果您想保留它們,請爲formCtrl定義控制器或從模板中刪除控制器使用。