简体   繁体   English

angularjs问题(错误捕获不是一个函数)

[英]angularjs issue with (error catch is not a function)

Hi I need some minor direction. 嗨,我需要一些小指导。 I can pull the data in the viewCtrl but every time i go to add a new contact, the issue I get is $http(..)then(..)catch is not a function at oBJECT.$scope.addContact I have no idea what is causing this? 我可以在viewCtrl中提取数据,但是每次我添加一个新联系人时,得到的问题是$ http(..)then(..)catch在oBJECT。$ scope.addContact中不起作用。知道是什么原因造成的? Its also not allowing me to "POST" but I am able to "GET". 它也不允许我“发布”,但我能够“获取”。 Can anyone see whats wrong with what i am doing? 谁能看到我的工作出了什么问题?

  var app = angular.module('app', []); app.controller('viewCtrl', function($scope, $http) { var url = "https://"; $http({ method: "GET", url: url, headers: { "accept": "application/json;odata=verbose" } }).success(function(data, status, headers, config) { $scope.contacts = data.d.results; console.log($scope.contacts); }).error(function(data, status, headers, config) {}); }); app.controller('addItemsController', function($scope, $http) { var url = "https://"; $scope.addContact = function() { return $http({ headers: { "Accept": "application/json; odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val() }, method: "POST", url: url, data: { 'Lastname': $scope.Lastname, 'Firstname': $scope.Firstname } }) .then(saveContact) .catch(function(message) { console.log("addContact() error: " + message); }); function saveContact(data, status, headers, config) { alert("Item Added Successfully"); return data.data.d; } } //console.log("an item has been added!"); }); app.controller('editItemsController', function($scope) { $scope.editItem = function() { console.log("an item can now be edited!"); } }); app.controller('deleteItemsController', function($scope) { $scope.deleteItem = function() { console.log("item has been deleted"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <!DOCTYPE html> <html ng-app="app"> <body ng-app="myapp"> <div ng-controller="viewCtrl"> <div ng-repeat="contact in contacts"> {{contact.ID}}: {{contact.Lastname}}, {{contact.Firstname}} <button>Edit</button> <button>Delete</button> <br /> </div> </div> <h3>Add Contacts</h3> <div ng-controller="addItemsController"> <div class="Table"> <div class="Row"> <div class="Cell"> First Name : </div> <div class="Cell"> <input type="text" id="Firstname" ng-model="Firstname" /> </div> </div> <div class="Row"> <div class="Cell"> Last Name : </div> <div class="Cell"> <input type="text" id="Lastname" ng-model="Lastname" /> </div> </div> <div class="Row"> <div class="Cell"> </div> <div class="Cell"> <input type="button" id="btnAddContact" value="Add Contact" ng-click="addContact()" /> <input type="button" id="btnAddContact2" value="Add Contact" ng-click="addItem()" /> </div> </div> </div> </div> <hr /> <h3>Edit Contacts</h3> <div ng-controller="editItemsController"> <div class="Table"> <div class="Row"> <div class="Cell"> ID : </div> <div class="Cell"> <input type="text" id="itemId" ng-model="itemId" /> </div> </div> <div class="Row"> <div class="Cell"> First Name : </div> <div class="Cell"> <input type="text" id="firstName" ng-model="firstName" /> </div> </div> <div class="Row"> <div class="Cell"> Last Name : </div> <div class="Cell"> <input type="text" id="lastName" ng-model="lastName" /> </div> </div> <div class="Row"> <div class="Cell"> </div> <div class="Cell"> <input type="button" id="btnEditContact" value="Edit Contact" ng-click="editItem()" /> </div> </div> </div> </div> <hr /> <h3>Delete Contacts</h3> <div ng-controller="deleteItemsController"> <div class="Table"> <div class="Row"> <div class="Cell"> ID : </div> <div class="Cell"> <input type="text" id="itemId" ng-model="itemId" /> </div> </div> <div class="Row"> <div class="Cell"> </div> <div class="Cell"> <input type="button" id="btnDelContact" value="Delete Contact" ng-click="deleteItem()" /> </div> </div> </div> </div> </div> </body> </html> 

I see some mismatch between function on onviewCtrl 我在onviewCtrl上看到函数之间有些不匹配

$http(...).success(...).error(...) $ HTTP(...)。成功(...)。错误(...)

while on addItemsController 而在addItemsController上

$http(...).then(...).catch(...) $ HTTP(...),然后(...)赶上(...)

use which code you are available on 使用您可以使用的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM