简体   繁体   中英

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? 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

$http(...).success(...).error(...)

while on addItemsController

$http(...).then(...).catch(...)

use which code you are available on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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