AngularJS - 在$资源之后更新模型,使用相同的控制器和#34;这个"或$ scope

时间:2016-03-17 14:05:06

标签: angularjs angular-ui-router

我有以下代码:

// States with same templates and a param to know the difference

[...]
.state('concluded', {
    url: '/concluded',
    params: { type: 'concluded' },
    parent: 'aggregators',
    templateUrl: 'views/aggregators/index.html',
    controller: 'AggregatorsCtrl'
})
.state('pending', {
    url: '/pending',
    params: { type: 'pending' },
    parent: 'aggregators',
    templateUrl: 'views/aggregators/index.html',
    controller: 'AggregatorsCtrl'
}
[...]

// Controller
var vm = this;
var type = $state.params.type;
vm.processos = [];

$scope.$watch(type, function () {    
    if (type === 'concluded') {
        getConcludedProcesses();
    } else if (type === 'pending') {
        getPendingProcesses();
    }
});

function getPendingProcesses() {
    // Resource call
    ProcessesService.getProcessosPendentes({ qtdDias: 12 }, function (response) {
        updateProcessos(response);
    });
}

function updateProcessos(response) {
    var processos = [];
    angular.forEach(response, function (e, key) {
        processos.push({
            [..]
        });
    });
    vm.processos = processos;
}

问题:当我第一次处于状态待处理时,一切都正常加载并且视图会更新。当我将状态更改为结束并更新 vm.processos 变量时,视图不会更新。但是,当我将 vm.processos 更改为 $ scope.processos 时,视图会更新。在问题上下文中使用 而不是 $ scope 有什么问题和区别?

我尝试使用 this 而不是 $ scope 来按照Angular的最佳做法对变量进行建模。

修改1

当使用这种方式时,我使用Controller作为synthax。

ng-controller="AggregatorsCtrl as aggregator"
[...]
ng-repeat="processo in aggregator.processos"
[...]

0 个答案:

没有答案
相关问题