Url已更改,但AngularJS中没有显示任何内容

Url changed but nothing shows up in AngularJS

本文关键字:显示 任何内 AngularJS Url      更新时间:2023-09-26

我有一个登录页面和一个主页,实现它们的方式是每个页面都有自己的模块。登录页面有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-->

我的初始url是:

http://localhost:8000/public_html/

在我点击提交(登录)后,它变为

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

但是视图不会改变,除非我刷新页面。还有另一篇关于这件事的帖子,但他错别字了,我确信我确实正确地键入了"templateUrl"。我甚至不知道是什么原因导致了这个错误。我只是假设ngRoute。

您错用了'.etheres'

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

模板url是指向html文件的url,该文件将被注入ng-view指令

路径是告诉路由器应该使用哪个模板url(和其他配置)的url的surfix

例如:

应用程序配置块:

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

控制器:

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