AngularJS,运行中的$ scope

时间:2014-02-27 10:44:42

标签: angularjs scope

我是AngularJS的新手。

实际上我有一个所有控制器都有的功能,所以我把这段代码保存在run方法中。该条码指定在满足条件时导航到其他页面,但是我的$location.path()无法正常工作,所以我保持$scope.$apply之后它让我导航...(当我保留时,这是有效的它在控制器中。)

但是当我把它投入运行时,它的投掷错误大约是$scope(但是没有$ scope没有导航)。

 if(prevPath=='/menupage')
        {

                $location.path('/restaurant');
                $scope.$apply() ;
                    }

        else if(prevPath=='/restaurant')
        {

                $location.path('/login');
        $scope.$apply() ;

        }

        else if(prevPath=='/menucategory')
        {

                $location.path('/menupage');
        $scope.$apply() ;       
        }

        else if(prevPath=='/menucategorylist')
        {

                $location.path('/menucategory');
        $scope.$apply() ;
        }

        else if(prevPath=='/shoppingcart')
        {

                $location.path('/menucategory');
            $scope.$apply() ;
        }

        else (prevPath=='/item')
        {

                $location.path('/menucategory');
        $scope.$apply() ;

        }

我认为除了运行代码之外别无选择......但$scope无效。

是否还有其他方法,例如将该代码放入函数并从run调用 (我们可以调用运行中的服务方法吗?)

1 个答案:

答案 0 :(得分:0)

您需要使用$route.reload();

而不是$scope.$spply();

if(prevPath=='/menupage')
        {

                $location.path('/restaurant');
                $route.reload();
                    }
.
.
.
  

并且必须在全局

中指定$route