无法访问angularjs中范围之外的全局变量

时间:2013-11-03 11:34:18

标签: angularjs

我一直在尝试访问angularJS的rootScope中的变量但是无法这样做。 我需要能够访问使用ng-app的DIV之外的selectedIndex2。 请帮忙。

AngularJS控制器:

<script>

    var navTabModule = angular.module("navTab",[])

    navTabModule.run(function ($rootScope) {
        $rootScope.selectedIndex2; 
    });

    navTabModule.controller("NavTabController",
        function($scope, $rootScope) {

            $scope.items = [
                {product_name: "dashboard"},
                {product_name: "contacts"},
                {product_name: "appointments"},
                {product_name: "todos"},
                {product_name: "transactions"},
                {product_name: "items"},
                {product_name: "calendar"},
                {product_name: "users"},
                {product_name: "reports"}
            ];

            $scope.selectedIndex = 0;

            $scope.itemClicked = function($index){
                $scope.selectedIndex = $index;
                $rootScope.selectedIndex2 = $index;
            }

            $scope.myFirstFunction = function(msg) {
                alert(msg+'try global variable');
            };
       });

</script>

HTML内容:

<div ng-app="navTab" ng-controller="NavTabController">
    <nav id="App1">
        <a href="#" ng-repeat="item in items"
           ng-class="{'selected-class-name':$index == selectedIndex}"
           ng-click="itemClicked($index)">
            {{item.product_name}}
        </a>
    </nav>

    {{selectedIndex}}
    {{selectedIndex2}}

</div>

{{selectedIndex2}}

0 个答案:

没有答案