使用$ location.path(..)

时间:2016-05-18 22:31:27

标签: javascript html angularjs ngroute angularjs-ng-route

我尝试实施an answer on another StackOverflow question,使用标题中的链接重定向Index.html的ng-view。

通过以下方式直接加载页面:http://server/#/page该应用程序可以正常运行(没有控制台错误,页面呈现正确,angular / js逻辑正确运行)。我的路由逻辑返回' undefined'当我逐步完成逻辑(进入角度库)时,将我传送到http://server/#,这是一个非常好的页面,除了页眉和页脚正确呈现。

我不确定我做错了什么。

我会选择性地将代码包括在内(只是为了保持合理的长度),但如果我遗漏了重要内容,请不要犹豫,请求。

的index.html:

<!DOCTYPE html>
<html ng-app="passwordResetApp">
<head>
    <title>COP Azure B2B Password Reset</title>
    <meta charset="utf-8"/>
    <!--Lib-->
        <!--ng base-->
            <script src="app/lib.bower/angular/angular.js"></script>
            <script src="app/lib.bower/angular-route/angular-route.js"></script>   
        <!--ui grid-->
            <script src="app/lib.bower/angular-ui-grid/ui-grid.min.js"></script>
            <link href="app/lib.bower/angular-ui-grid/ui-grid.min.css" rel="stylesheet"/>
        <!--Misc-->
            <link rel="stylesheet" href="app/lib.bower/bootstrap/dist/css/bootstrap.min.css" />
    <!--Custom-->
    <script src="app/app.js"></script>
    <!--Custom - Services-->
    <script src="app/services/resetRequestService.js"></script>
    <script src="app/services/adValidationService.js"></script>
    <!--Custom - Controllers-->

    <script src="app/controllers/HeaderController.js"></script>
    <script src="app/controllers/PasswordResetRequestController.js"></script>
    <!--Custom modules-->
    <script src="app/modules/header.js"></script>
    <!--Custom - Other-->
    <link rel="stylesheet" href="app/css/passwordReset.css"/>

</head>
<body ng-app>
<div class="container">
    <!--<div ng-include="'app/views/_header.html'"></div>-->
    <div header></div>
    <div class="viewWrapper">
        <div ng-view></div>
    </div>
    <div class="push"></div>
</div>
<div ng-include="'app/views/_footer.html'"></div>
</body>
</html>

app.js:

(function() {
    'use strict';

    var app = angular.module('passwordResetApp', ['ngRoute']);

    app
        .config(
        function($routeProvider) {
            $routeProvider.when('/', {
                templateUrl: 'app/views/passwordReset.html',
                controller: 'ResetRequestController as vm',
                caseInsensitiveMatch: true
            }).when('userSearch', {
                templateUrl: 'app/views/userSearch.html',
                controller: 'ResetRequestController as vm',
                caseInsensitiveMatch: true
            }).otherwise({ redirectTo: '/' });
        }
    );

    app.directive('header',
        function() {
            return {
                restrict: 'A',
                //This menas that it will be used as an attribute and NOT as an element. I don't like creating custom HTML elements
                replace: true,
                templateUrl: 'app/views/_header.html',
                controller: 'HeaderController as vm',
                caseInsensitiveMatch: true
            }
        });

    app.run([
        '$route', '$http', '$rootScope',
        function ($route, $http, $rootScope) {
            $http.defaults.withCredentials = true;
            $rootScope.getUrlPath = function (url) {
                return baseUrl + url;
            };
        }
    ]);
}());

_header.html:

<header id="pageHeader">
    <div class="row">
        <div class="col-sm-4 col-md-3 col-lg-2 cop-logo-container">
            <img id="cop-logo" src="app/img/ConocoPhillips_Logo.png" />
        </div>
        <div class="col-sm-4 col-md-6 col-lg-8">
            <h2>Azure B2B Password Reset</h2>
            {{vm.currentUser.name}}
        </div>
        <div class="col-sm-4 col-md-3 col-lg-2">
            <div class="pull-right" style="padding: 20px">
                <div ng-if="vm.currentUser" id="headerUser">
                    <p>Welcome, <span class="username">{{vm.currentUser.name}}</span></p>
                </div>
                <div ng-if="!vm.currentUser">
                    <div class="btn btn-primary">Login</div>
                </div>
            </div>
        </div>
    </div>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <ul ng-if="vm.currentUser" class="nav navbar-nav">
                    <li><a href="#" ng-click="vm.changeView('home')">Home</a></li>
                    <li><a href="#" ng-click="vm.changeView('userSearch')">Search Users</a></li>
                    <li><a href="#" ng-click="vm.logout()">Logout</a></li>
                </ul>
                <ul ng-if="!vm.currentUser" class="nav navbar-nav">
                    <li><a href="#" ng-click="vm.changeView('home')">Home</a></li>
                </ul>
            </div>
        </div>
    </nav>
</header>

HeaderController.js:

(function() {
    'use strict';

    var app = angular.module('passwordResetApp');

    var headerController = function ($scope, $location) {
        var vm = this;
        vm.currentUser = {};
        vm.currentUser.name = 'caninc';

        vm.changeView = function(view) {
            $location.path(view);
        }
    };

    app.controller('HeaderController', headerController);


}());

1 个答案:

答案 0 :(得分:2)

问题出在<li><a href="#" ng-click="vm.changeView('home')">Home</a></li>

#所以点击http://server/#时不是http://server/#/page