Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular Js location.path仅使用刷新按钮_Angularjs - Fatal编程技术网

Angularjs Angular Js location.path仅使用刷新按钮

Angularjs Angular Js location.path仅使用刷新按钮,angularjs,Angularjs,appController.js $scope.showPageHero = $location.path() ; $routeProvider. when('/login', { templateUrl: 'login.html', controller: 'LoginController' }). when('/view', { templateUrl: 'view.

appController.js

$scope.showPageHero = $location.path() ;
  $routeProvider.
        when('/login', {
            templateUrl: 'login.html',
            controller: 'LoginController'
        }).
        when('/view', {
            templateUrl: 'view.html',
            controller: 'viewController',
            auth: true
        }).
        when('/details', {
            templateUrl: 'details.html',
            controller: 'detailsController',
            auth: true
        })
app.js

$scope.showPageHero = $location.path() ;
  $routeProvider.
        when('/login', {
            templateUrl: 'login.html',
            controller: 'LoginController'
        }).
        when('/view', {
            templateUrl: 'view.html',
            controller: 'viewController',
            auth: true
        }).
        when('/details', {
            templateUrl: 'details.html',
            controller: 'detailsController',
            auth: true
        })
index.jsp

<li><a ng-href="#/login" >Login</a></li>
<li><a ng-href="#/view" >view</a></li>
<li><a ng-href="#/details" >Global</a></li>
.
.

<fieldset ng-if="showPageHero == '/view'">
<h1><span>Hi... {{showPageHero}} </span></h1> 
</fieldset>
  • . . 你好{{showPageHero}}
    我正在使用
    $location.path()
    查找应用程序的当前url,当我直接点击url或刷新页面时,它就会工作。但当通过网页中的
    href
    单击相同的按钮时,它不起作用。无论如何都有办法解决这个问题。谢谢

    线路

    $scope.showPageHero = $location.path();
    
    当appController实例化时,在应用程序的最开始执行一次。因此,它获取当时的位置,并将其存储在一个变量中。变量在之后不会更改

    所以你有两个选择:

    • 订阅位置更改事件,以便在每次位置更改时更新此变量
    • 询问每个摘要循环的当前路径
    第二个更简单(虽然效率稍低):


    尝试放置
    $scope.showPageHero=$location.path()内部
    viewController
    ,因为它是该视图的负责控制器。