【发布时间】:2014-03-28 23:41:59
【问题描述】:
尽管这个Data is not getting updated in the view after promise is resolved 有一个类似的问题,但我已经在使用这种方法并且视图没有被更新。
我有一家工厂:
'use strict';
myApp
.factory('Factory1', [ '$http','$q','$location', '$rootScope', 'Service', function($http, $q, $location, $rootScope, Service){
return {
checkSomething: function(data){
var deferred = $q.defer(); //init promise
Service.checkSomething(data,function(response){
// This is a response from a get request from a service
deferred.resolve(response);
});
return deferred.promise;
}
};
}]);
我有一个控制器:
'use strict';
myApp
.controller('MyCtrl', ['$rootScope', '$scope', '$location','Service', 'Factory1' function($rootScope, $scope, $location, Service, Factory1) {
if(Service.someCheck() !== undefined)
{
// Setting the variable when view is loaded for the first time, but this shouldn't effect anything
$scope.stringToDisplay = "Loaded";
}
$scope.clickMe = function(){
Factory1.chechSomething($scope.inputData).then(function(response){
$scope.stringToDisplay = response.someData; // The data here is actually being loaded!
});
};
}]);
还有观点:
<div class="app " ng-controller="MyCtrl">
{{stringToDisplay}}
<button class="button" ng-click="clickMe()">Update display</button>
</div>
但是当我单击“更新显示”按钮时,视图中的数据没有更新。为什么?
即使$scope 正在加载数据
编辑:
嗯,我在尝试$scope.$apply() 时似乎遇到了错误,它说:
[$rootScope:inprog] $digest already in progress
【问题讨论】:
标签: angularjs