0

I am at the url path http://localhost:3000/u#/ and I click on an href link to go to http://localhost:3000/u#/76457898/profile. I need this to happen without sending a request to server. But that only happens the second time I click on the href link.

The first time I click on it, I get myself redirected back to http://localhost:3000/u#/76457898. Only the second time I click on it the page is not redirected and I get only the users.profile state takes the <div ui-view><div>.

The http://localhost:3000/u#/ takes me to the same page as http://localhost:3000/u#/76457898.

HTML

<a href="{{goToUIState('users.profile', user.userId)}}">
    <!--stuff-->
</a>

AngularJS with Angular UI Router

.state('users', {
    url: '/:userId',
    abstract: true,
    resolve: {
        user: ['$cookies', '$stateParams', '$http', 'localStorageService',
            function($cookies, $stateParams, $http, localStorageService) {
                var uid = $cookies.uid || localStorageService.get('uid');
                return $http.get('/api/users/' + uid).success(function (resp) {
                    localStorageService.set('uid', uid);
                    return resp;
                })
            }
        ]
    },
    views: {
        'home': {
            templateUrl: '/partials/user/index.html',
            controller: ['$scope','$state', 'user', '$stateParams', function($scope, $state, user, $stateParams) {
                $scope.user = user.data;

                //dynamic ui-sref do not work, so creating a function to do that with $state.href()
                $scope.goToUIState = function(state, userId) {
                    return $state.href(state, {userId: userId});
                }
                $state.go('users.home');
            }]
        }
    }
})

.state('users.home', {
    url: '',
    templateUrl: '/partials/user/users.home.html',
    controller: [$scope', 'user', function($scope, user) {
        $scope.user = user.data;
    }]

})

.state('users.profile', {
    url: '/profile',
    templateUrl: '/partials/user/users.profile.html',
    controller: ['$scope', '$rootScope', 'user', '$http', 'fileUpload',
        function($scope, $rootScope, user, $http, fileUpload) {
            $scope.user = user.data;
            //stuff
        }]
})

Could somebody help me understand the reason behind why do I get the desired second only when I click the href link second time and how do I resolve this?

I am using NodeJS/ExpressJS on the server side.

4

1 回答 1

1

href我认为由于属性内部使用的函数,您遇到了这个问题。前段时间我遇到了类似的问题(我也在开发一个应用程序,我正在使用 ui-router)。问题在于在标签内使用href。

我认为这个问题可能会有所帮助。了解浏览器如何解释空href属性很重要。

尝试在其他标签中使用您的功能,比如说在ul列表和ui元素(ng-click指令)中创建一个菜单。它对我的情况有所帮助。

于 2014-11-05T22:42:02.877 回答