1

定义控制器的配置函数:

function config($stateProvider){

    $stateProvider
    .state('games', {
        url: '/games',
        templateUrl: 'client/games/games.ng.html',
        controller: 'Games',
        controllerAs: 'vm'
    });
}

我的控制器:

function GamesController($scope, $meteor, Engine) {
var vm = this;
vm.selectedMove = { _id: 'dsfsdf'}
vm.evaluations = $meteor.collection(Evaluations),
$meteor.subscribe('evaluations', {}, $scope.getReactively('vm.selectedMove')._id).then(function(){
    console.log(vm.evaluations);
});
}

我收到一条错误消息:“无法读取未定义的属性 '_id'”,它指向这一行:'$scope.getReactively('vm.selectedMove')._id).then...'

我究竟做错了什么?提前致谢!

4

1 回答 1

1

绑定的值$scope.getReactively可能并不总是存在。在这种情况下,您在构建控制器时调用 getReactively。所以$scope.vm还没定。因此 getReactively 将返回 undefined。

您可以尝试$scope.getReactively('vm.selectedMove._id')检查未定义的。例如:

this.query = {q : '' };
this.list = $scope.$meteorCollection(Participants);
$scope.$meteorAutorun(function() {
    var q = $scope.getReactively('participants.query.q');
    $scope.$meteorSubscribe('participants', q || '')
});

另请注意,整个事情必须包装在自动运行中。

于 2015-08-21T18:50:28.667 回答