javascript - 网址已更改,但 AngularJS 中没有显示任何内容

标签 javascript angularjs ngroute

我有一个登录页面和一个主页,我实现它们的方式是每个页面都有自己的模块。登录页面有loginApp,主页有myApp。当我单击提交时,我想导航到 home.html。

在我的index.html(登录页面)中,我有这个:

   <script>
     angular.module("whole", ["loginApp", "myApp"]);
   </script>

在顶部我声明了这一点:

<html ng-app="whole" class="ng-scope">

现在我被困在这个 ngRoute 上。我有这个:

"use strict";
var lapp = angular.module("loginApp", ["ngRoute", "ngCookies"]);

lapp.config(["$routeProvider", function ($routeProvider) {
    $routeProvider
            .when("/", {
                templateUrl: "index.html"
            })
            .when("/home", {
                templateUrl: "home.html"
            })
            .otherwise({redirectTo: '/'});
}]);

这在我的登录Ctrl中:

$scope.login = function () {
        var credentials = {
            username: $scope.username,
            password: $scope.password
        };
        Auth.login(credentials, function (res) {
            init();
            $location.path("views/home.html");
        }, function (err) {
            init();
        });

这在我的 html 中:

<div ng-controller="loginCtrl" class="login">
  <form ng-submit="login()" method="post" name="form" novalidate>  
   <!--two inputs and stuff-->
   <input type="submit" value="Login">
 <!--Then some closing tags, whatever-->

我的初始网址是:

http://localhost:8000/public_html/

点击提交(登录)后,它变成了

http://localhost:8000/public_html/views/home.html#/basic

但是除非我刷新页面,否则 View 不会改变。 还有另一篇关于此的文章,但他犯了一个拼写错误,我确信我确实正确地输入了“templateUrl”。我什至不知道是什么导致了这个错误。我只是假设 ngRoute。

最佳答案

你误用了“.otherwise”

您需要区分 templateUrl 和 path 之间的理解。

A template url is a url points on an html file that will get injected into the ng-view directive

A path is your surfix of the url telling your router which templateUrl (and other configs) should be used

例如:

应用程序配置 block :

$routeProvider
   .when("/login", {
         templateUrl: "login.html"
     })
   .when("/home", {
         templateUrl: "home.html"
     })
     .otherwise({redirectTo: '/home'}); <-- if no path is matched, use this path 

Controller :

  $scope.login = function () {
    var credentials = {
        username: $scope.username,
        password: $scope.password
    };

    Auth.login(credentials, function (res) {
        init();
        $location.path("/home");
    }, function (err) {
        init();
    });

关于javascript - 网址已更改,但 AngularJS 中没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28453532/

相关文章:

css - 禁用并在视觉上禁止打开模式对话框的按钮

javascript - ngRoute 不工作

javascript - 带有代理脚本的 IMacros

javascript - 从 Webkit 通知返回给 parent

javascript - Scriptaculous Effect.Grow() 从设定大小开始,而不是 0

javascript - 使用对象相同属性的多个值进行过滤

javascript - 类型错误 : Cannot read property '$valid' of undefined on using ng-submit

Javascript 代码未在 PUG/Jade 文件上运行

javascript - Angular.js 应用程序 : eliminate #! 来 self 的单个帖子的 URL,同时保持 slug