Angularjs 使用Restanglar成功完成修补程序后,获取要更新的$scope.object
我在列表中有一个对象。称之为$scope.contact 我做了一个补丁到终点,它成功返回。但是,我不知道如何使用新信息更新$scope.contact 这是我的第一个完整应用程序,所以我肯定我犯了一个愚蠢的错误。请随意链接到我错过的文档或我得不到的原则 这是目前为止我的控制器:Angularjs 使用Restanglar成功完成修补程序后,获取要更新的$scope.object,angularjs,angularjs-scope,restangular,Angularjs,Angularjs Scope,Restangular,我在列表中有一个对象。称之为$scope.contact 我做了一个补丁到终点,它成功返回。但是,我不知道如何使用新信息更新$scope.contact 这是我的第一个完整应用程序,所以我肯定我犯了一个愚蠢的错误。请随意链接到我错过的文档或我得不到的原则 这是目前为止我的控制器: contactControllers.controller('ContactDetailController', ['$scope', 'growl', 'Restangular', function($scope,
contactControllers.controller('ContactDetailController', ['$scope', 'growl', 'Restangular', function($scope, growl, Restangular) {
$scope.editContact = function() {
Restangular.one('contacts', $scope.contact.id).patch($scope.contact).then(function(contact) {
$scope.contact = contact;
// I can't figure out what is supposed to go here.
// If I do .push(contact) I get undefined errors.
growl.addSuccessMessage("Your contact was edited!");
},function(response) {
console.log('Error with status code: ', response.status);
});
}
}])
更新:因为它不是一个数组,它是一个对象,这就是控制器方法 然后wrap scope.vontact=联系并应用如下方法
$scope.$apply(function() {
$scope.contact=contact;
});
因此,完整的控制器将是
contactControllers.controller('ContactDetailController', ['$scope', 'growl', 'Restangular', function($scope, growl, Restangular) {
$scope.editContact = function() {
Restangular.one('contacts', $scope.contact.id).patch($scope.contact).then(function(contact) {
$scope.$apply(function () {
$scope.contact = contact;
});
// I can't figure out what is supposed to go here.
// If I do .push(contact) I get undefined errors.
growl.addSuccessMessage("Your contact was edited!");
},function(response) {
console.log('Error with status code: ', response.status);
});
}
}])
看起来$object会有所帮助,但似乎没有一个好的方法来捕捉错误,或者做任何有条件成功的事情。如果$object是正确的方法,那么一个错误将如何检查或执行条件呢$对象不正确,默认情况下是数组。它不是数组。它是一个物体。这就是我使用restanglar.one的原因——它只是一个对象。您的示例对于列表(推送或调用)非常有效,但这不是我想要做的。此外,可能是不相关的,但当我首先设置数组时,我得到了不允许的方法,我认为这是因为它正试图发送和数组到一个对象,如果您将其设置为数组,然后在$scope.contact.id上执行.one,它现在为空,因为您重置了它。如果两者都是对象,则不重置它,如果你得到了联系,那么如果它没有得到范围。联系被升级,那么他们是一种我将在回答中更新的方式。使用apply给我一个已经在进行中的摘要错误。阅读本文后:这个错误是有意义的,因为表单提交事件中已经发生了$apply。哦,好吧……然后我们需要创建一个角度工厂,将restangular调用包装在其中,然后在回调中分配它