2017-06-23 29 views
0

嗨,我需要一些小的方向。我可以拉ViewCtrl中的数据,但每次我去添加一个新的联系人时,我得到的问题是$ http(..)然后(..)catch在oBJECT不是一个函数。$ scope.addContact我没有想法是什么造成这种情况? 它也不允许我“发布”,但我能够“获得”。任何人都可以看到我在做什么错?angularjs问题(错误捕获不是函数)

 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>

+0

什么**正**是完整的错误信息? – Phil

+0

angular.min.js:63 TypeError:$ http(...)。then(...)。catch不是函数 at Object。$ scope.addContact – Vzupo

回答

0

我看到函数之间的一些不匹配的 onviewCtrl

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

同时addItemsController

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

使用哪些代码可用

+0

这两种样式都可以在AngularJS 1.2.0中使用 – Phil

+0

什么你的意思是风格? – Vzupo

+0

https://plnkr.co/edit/1yEUxORsew2O74T0RjXV?p=preview 我使用这个运动器,并调整角度版本。你可以试试 –

相关问题