【发布时间】:2015-09-29 20:32:31
【问题描述】:
在将许多 google 结果链接变为紫色后。我发现自己被难住了。
我设置了一个 $resource 用于与我设置的 contacts api 端点(Laravel 后端)进行交互。查询要在页面上显示的初始联系人非常有用。我什至可以发送要创建的新联系人,但是在我发送 POST 请求以创建新联系人后,联系人并没有更新。
联络人 var contactCtrl = angular.module('contactCtrl', ['ngResource']);
contactCtrl.controller('contactController', ['$scope', '$http', 'Contact', 'ContactRest', 'routeBase', function($scope, $http, Contact, ContactRest, routeBase) {
$scope.contacts = [];
// $scope.contacts is updated with all of the contacts for /activity/1
ContactRest.getResource({'resource_type' : 'activity', 'resource_id' : '1'}, function(contacts) {
$scope.contacts = contacts;
}, function() {
console.log('contacts loading failed');
});
$scope.contact = new ContactRest();
// Action to send a request to create a new contact for /activity/1
$scope.add = function(contact, resource_type, resource_id) {
console.log('submitting the contact: ', contact);
contact.$add({ 'resource_type' : resource_type, 'resource_id' : resource_id });
$scope.contacts.push(contact);
console.log('current contacts in scope are', $scope.contacts);
$('#contactModal').modal('hide');
}
}]);
联系 Restful 服务
.factory('ContactRest', ['$resource', '$location', function($resource, $location) {
var host = $location.host()
baseUrl = 'http://' + host;
return $resource('http://' + host + '/api/contact', { }, {
// Get all contacts belonging to a resource
getResource : {
method : 'GET', url : 'http://' + host + '/api/:resource_type/:resource_id/contact',
params : {'resource_type' : '@resource_type' , 'resource_id' : '@resource_id' },
isArray: true,
},
// Add a contact to a resource
add: {
method : 'POST',
url : baseUrl + '/api/contact/add/:resource_type/:resource_id',
params : {'resource_type' : '@resource_type', 'resource_id' : '@resource_id'} }
});
}]);
$scope 在我发送请求后更新。我已经测试过几次了。似乎视图中的中继器不会随着控制器中的 $scope.contacts 更新而改变。我有什么遗漏吗?
【问题讨论】:
-
代替
$scope.contact = new ContactRest();做$scope.contact = ContactRest;
标签: angularjs angularjs-scope angularjs-ng-repeat angular-resource