AngularJS Route显示下一个视图,然后发送回上一个视图

时间:2016-05-19 19:39:42

标签: angularjs angularjs-ng-route angularjs-1.5

使用我认为的基本路线,但它不喜欢我。

    .config([
        '$routeProvider',
        function($routerProvider){
            $routerProvider
                .when('/view',{
                    templateUrl: 'view-profile.html',
                    controller: 'UserProfileController'
                })
                .when('/edit',{
                    templateUrl: 'edit-profile.html',
                    controller: 'UserProfileController'
                })
                .otherwise({
                    redirectTo: '/view'
                });
        }
    ]);

以上工作,我可以在浏览器中手动输入/查看或/编辑,它将加载,并工作。

我的页面上有按钮来切换UI,当我这样做时,我会看到新视图,然后立即切换回来。我在/查看,我点击更改路径进行编辑,然后进行编辑,然后返回查看。但是,当我开始编辑,然后单击以更改为查看时,它会坚持下去。然后我可以回去编辑。它似乎是在页面加载视图时。

以下是我改变观点的方式

    this.viewProfile = function () {
        $location.path('/view');
    };
    this.editProfile = function () {
        $location.path('/edit');
    };

按钮看起来像

开启/查看

<button ng-click="user.editProfile()">Edit My Profile</button>

开/编辑

<button ng-click="user.viewProfile()">Save</button>

谢谢!

2 个答案:

答案 0 :(得分:0)

你试过&#34; window.location.href&#34;?

window.location.href = "#/view";

或申请?

$scope.$apply(function() {
  $location.path('/view');
});

或有超时的东西

$timeout(function () {
    $location.path('/bar');
});

答案 1 :(得分:0)

我认为你有2种选择。您可以使用href标记指向您想去的地方

,而不是按钮
<a href="/edit">Edit My Profile</a>

另一个选择是在$ location.path()之后使用$ scope。$ apply()。我认为它工作正常,但我认为第一个选择更好。