当我执行范围函数并尝试在执行服务后更新值时,数据不会更新。
在函数内部,服务返回显示正确。
以下代码是我的控制器代码:
angular.module('myApp').controller('MyControllerCtrl', function ($scope, $sce, $location, $routeParams, ClientService) {
'use strict';
$scope.returnClient = {};
$scope.row = {id: false};
$scope.rowClient = {name: ''};
$scope.postClient = function () {
ClientService.post($scope.rowClient, function (data) {
var item = {};
if (data.error) {
showError(data.message);
} else {
showSuccess(data.message);
item = {
id: data.id,
text: $scope.rowClient.name
};
$scope.returnClient = item;
$scope.row.codClient = item.id;
}
});
};
});
服务代码:
angular.module('myApp').service('ClientService', function ClientService($http) {
'use strict';
this.post = function (row, callback) {
try {
$http.post(getSiteUrl('client/create'), row).success(callback).error(function () {
showError('Error.');
});
} catch (errorPost) {
showError('Error.');
}
};
});
答案 0 :(得分:0)
问题解决了:
HTML包含两次相同的控制器。
答案 1 :(得分:0)
我建议你使用角度资源。这是使用RestFull的最好方法。如果你使用ngResource,你不必在“this”或其他东西上声明一个函数.Resource有完美的url导航。你可以用不同的方法,不同的网址等创建功能。