0

嗨,我是 angularjs 的新手,我正在使用 $rootScope 来更改 $stateChangeSuccess 上的 $scope 变量。问题是,我收到一条错误消息,上面写着“TypeError:无法设置属性 'show' of null”。这是我的代码片段

    // unhide this view whenever visited
    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState,     fromParams) {
        $scope.$parent.show = false;
        alert($scope.$parent.show);
        return $scope.$parent.show;

});

和html ...

<div class="row">
    <div class="dl-horizontal" id="information">
        <div class="col-md-8 col-md-offset-2">

        <!-- route veiw, hide the child view initailly -->
        <div ui-view ></div>
    </div>

    <div class="col-md-8 col-md-offset-2" ng-hide="show">

基本上,每当我退出子状态时,我都想取消隐藏父视图。$scope 变量有一个值并且代码有效,只是我收到了这个错误消息。有关如何解决此错误的任何想法?

谢谢

4

2 回答 2

1

如果您想避免使用根范围,可以将 $on 添加到包含您希望更改的属性的 $scope 中。

例如:

controller: function($scope, $stateParams, carsDataService) {
    $scope.model = {};
    $scope.model.cars = carsDataService.getCars();
    $scope.pageState = { selectedCarId: null };

    $scope.$on('$stateChangeStart', function(event, toState, toParams,    fromState, fromParams){
        $scope.pageState.selectedCarId = toParams.id;
    });
}
于 2015-01-25T14:27:34.427 回答
0

You can't access a child scope inside a listener of the $rootScope.

Just set your property on the $rootScope and, if your scope isn't isolated, it will be accessible from children scopes.

于 2014-08-15T18:29:12.760 回答