即使这个
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','Factory1' function($rootScope,$scope,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时收到一个错误$apply(),它说:
[$rootScope:inprog] $digest already in progress