0

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

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

以上工作,我可以在浏览器中手动输入 /view 或 /edit ,它会加载并工作。

我的页面上有用于切换 UI 的按钮,当我这样做时,我会看到新视图,然后它会立即切换回来。我在 /view 上,我单击更改编辑路径,然后进入编辑然后返回查看。但是,当我开始编辑并单击以更改查看时,它会粘住。然后我可以回去好好编辑。它似乎是在页面加载时。

这是我改变观点的方式

    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>

谢谢!

4

2 回答 2

0

你试过“window.location.href”吗?

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

或与应用有关的东西?

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

或超时的东西

$timeout(function () {
    $location.path('/bar');
});
于 2016-05-19T19:46:58.977 回答
0

我认为您在这里有两个选择。除了按钮,您可以使用 with href 标签来指向您想去的地方

<a href="/edit">Edit My Profile</a>

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

于 2016-05-19T19:49:21.157 回答