http调用后如何在视图中更新作用域变量值? [英] How to update scope variable value in view after http call?
问题描述
我正在使用 Angularjs 1.4.3 创建我的第一个项目.
I am making my first project using Angularjs 1.4.3.
在我的控制器中,我正在发出 http 请求,在此http请求的成功方法中,我正在更新范围变量.在http调用中,我获取的是最新值,但在视图端,它没有更新值.
In my controller I am making a http request, in the success method of this http request I am updating a scope variable. In http call I am getting the latest values but in the view side its not updating the values.
柱塞链接 (感谢@rupesh_padhye). (因为它正在调用servlet操作,所以 Plunker 中将不会显示任何数据)
Plunker Link (@rupesh_padhye thanks). (Since it is calling the servlet action, so no data will be shown in Plunker)
app.controller('measuresCtrl', ['$scope', '$modal', '$http', function($scope, $modal, $http) {
$scope.groups = []
$scope.loadAllMeasure = function() {
$http.get("fetchAllMeasure")
.success(function(data) {
console.log("Before insert");
console.log($scope.groups);
$scope.groups = data.measures;
console.log("After insert");
console.log($scope.groups);
})
.error(function() {
});
};
$scope.loadAllMeasure();
$scope.submit = function (form) {
$http({
method: 'POST',
url: 'saveMeasure',
data: {
id: form.id,
name: form.name,
description: form.description
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {
$scope.loadAllMeasure();
}).error(function(data, status, headers, config) {
});
}
})
每当我对度量执行任何CRUD操作时,都会调用方法$scope.loadAllMeasure();
.但是它不会更新视图(jsp)页面中的值.
And whenever I am performing any CRUD operation on measures I am calling a method $scope.loadAllMeasure();
. But its not updating the values in the view (jsp) page.
我已经尝试过 $ scope.$ apply 方法,但是我正在获取Error: $digest already in progress
.
I have tried $scope.$apply method but I am getting Error: $digest already in progress
.
当我在成功方法内使用 console.log 打印 $ scope.groups 的值时,然后显示最新的值.
When I printed the value for $scope.groups using console.log inside success method, then its showing the latest values.
在我的视图(jsp)页面中,我只是使用 ng-repeat 函数以表格格式显示所有记录.
In my view (jsp) page I am just using ng-repeat function to show all the records in table format.
我的查看页面的代码(最低代码)-
<div ng-repeat="group in groups | orderBy:'name'">
<div>
<input type="checkbox" id="checkbox-{{group.id}}" class="ui-checkbox" /><label for="checkbox-{{group.id}}">{{group.name}}</label>
</div>
<div>{{ group.description}}</div>
<div>
<div class="fa fa-pencil button" data="{{group.id}}" id="{{::elementId}}" ng-click="showEditForm(group.id, $event)"></div>
<div class="fa fa-trash button" data="{{group.id}}" ng-click="deleteGroup(group.id)"></div>
<div class="fa fa-clone button" data="{{group.id}}" id="{{::elementId}}" ng-click="showCloneForm(group.id, $event)"></div>
</div>
</div>
console.log中的值
Before insert
Object { id=1, description="Measure Description1", name="Demo"}]
还有
After Insert
[Object { id=1, description="Measure Description1", name="Demo"}, Object { id=2, description="Description2", name="Demo2"}]
如何在http调用后更新视图中的范围变量值?
推荐答案
我看不到示例代码有什么问题.
I cant see anything wrong with your example code.
我创建了一个JSFiddle来尝试帮助您.
I have created a JSFiddle to try and help you.
服务器调用已被setTimeout函数替换,该函数返回了一个Promise.
The server call has been replaced by a setTimeout function that returns a promise.
请参阅JSFiddle https://jsfiddle.net/sjwkbzxa/
Please see JSFiddle https://jsfiddle.net/sjwkbzxa/
请参见以下示例:
<div data-ng-controller="TestController as vm">
<button data-ng-click="loadAllMeasure()">Load List from Server</button>
<ul>
<li data-ng-repeat="group in groups | orderBy:'name'">
<span>{{group.description}}</span>
</li>
</ul>
</div>
javascript:
The javascript:
angular.module('application',[]).controller("TestController", ['$scope', '$q', function($scope, $q){
$scope.groups = [{ id:1, description:"Initial List", name:"Demo"}];
$scope.loadAllMeasure = function(){
loadData().then(function(data){
$scope.groups = data;
});
};
function loadData(){
var deferred = $q.defer();
setTimeout(function(){
var data = [{ id:1, description:"Measure Description1", name:"Demo"}, { id:2, description:"Description2", name:"Demo2"}];
deferred.resolve(data);
}, 3000);
return deferred.promise;
}
}]);
也许您身边缺少我们看不到的东西?
Maybe you are missing something on your side that we cant see?
这篇关于http调用后如何在视图中更新作用域变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!