Angular Meteor controllerAs getReactively

Angular Meteor controllerAs getReactively

定义controller的config函数:

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);
});
}

我收到一条错误消息: "Cannot read property '_id' of undefined",它指向这一行: '$scope.getReactively('vm.selectedMove')._id).then...'

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

$scope.getReactively 绑定的值可能并不总是存在。在这种情况下,您在构建控制器时调用 getReactively。所以$scope.vm还没有设置。因此 getReactively 将 return 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 || '')
});

另请注意,整个内容必须包含在自动运行中。