-1
當我執行範圍函數並嘗試執行服務後更新值時,數據不會更新。範圍變量不執行更新後執行AngularJS範圍內的服務功能
在函數內部,返回的服務顯示正確。下面
的代碼是我的控制器代碼:
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.');
}
};
});
你可以發佈'ClientService.post'代碼嗎? – dfsq
@dfsq發佈服務代碼。 – Tomazio
定義'$ scope.rowClient'在哪裏?我看到你將它傳遞給'ClientService.post()'並訪問它的'.nome'屬性,但我沒有看到你在任何地方初始化它。 – JLRishe