【问题标题】:Angular Meteor controllerAs getReactivelyAngular Meteor 控制器作为 getReactively
【发布时间】:2015-07-03 03:09:53
【问题描述】:

定义控制器的配置函数:

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...'

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

【问题讨论】:

    标签: angularjs meteor angular-meteor


    【解决方案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 || '')
    });
    

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

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2015-02-11
      • 2015-11-30
      • 2015-09-01
      • 2016-04-11
      • 2015-07-19
      • 2017-05-02
      • 2017-04-08
      相关资源
      最近更新 更多