简体   繁体   English

AngularJS视图未使用UI-Router更新

[英]AngularJS View not updating using UI-Router

I have the following: 我有以下几点:

<div ng-repeat="m in model">
    {{m.prop1}}{{m.prop2}}
</div>

inside controller is a deffered promise that fetches an object from the server. 控制器内部是一个延迟承诺,它从服务器获取对象。

promise.then(function(e){
    $scope.model.push(e); // e is an object, which is pushed into the array.
    $scope.$apply(); // causes digest already in progress error so put inside $timeout
});

I've tried $timeout and $scope.$apply() and the view still wont update: 我已经尝试了$ timeout$ scope。$ apply() ,但视图仍然不会更新:

var promise = ModelService.create(model,data);
promise.then(function(e){
    $timeout(function(){
        $scope.$apply(function(){
            $scope.model.push(e);
        });
    })
});

Update: ModelService.js 更新:ModelService.js

create: function(model, data){
            var defer = $q.defer();
            $http.post('/api/'+model+"/create", data).success(function(e){
                defer.resolve(e);
            }).error(function(e){
                defer.reject(e);
            });
            return defer.promise;
        }

any ideas to get the view to render the model update? 有什么想法可以获取视图以渲染模型更新?

My problem was down to a controller problem and overlooked workings of ui-router 我的问题归结为控制器问题,而忽略了ui-router工作原理

I had 2 states, model and model.create , I declared both of these to use the same controller ModelController . 我有两个状态, modelmodel.create ,我声明了这两个状态以使用相同的控制器ModelController

When on ui-view for model.create it created a new instance of ModelController ui-view的model.create它创造的新实例ModelController

This meant that I was pushing the returned data into an array that wasnt rendered as it was rendered on the parent states instance of the controller. 这意味着我正在将返回的数据推入一个未呈现的数组,因为该数组在控制器的父状态实例上呈现。

To fix I passed false for the controller parameter of the child state causing it to use the same instance as the parent state and update the correct array. 为了解决这个问题,我为子状态的controller参数传递了false,导致其使用与父状态相同的实例并更新了正确的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM