简体   繁体   English

AngularJS在index.html中的双向绑定

[英]Angularjs two-way binding in index.html

I am new to Angularjs. 我是Angularjs的新手。 I am trying to build a simple system with a login page and it will redirect to another page after successfully logged in. 我正在尝试用登录页面构建一个简单的系统,成功登录后它将重定向到另一个页面。

I am trying to show all the pages in index.html with ng-view (including login page). 我正在尝试使用ng-view显示index.html中的所有页面(包括登录页面)。 I try to use ng-if to decide when to show the navigation bar on top of the page. 我尝试使用ng-if决定何时在页面顶部显示导航栏。

The value of vm.page will not change after logged in 登录后vm.page的值不会更改

index.html index.html

<html ng-app="myApp">
<head>
    <title></title>
    <link href="css/bootstrap.min.css" rel="stylesheet"/>
    <link href="css/custom.css" rel="stylesheet"/>

    <script src="jquery/jquery-3.3.1.min.js"></script>
    <script src="node_module/angular/angular.js"></script>
    <script src="node_module/angular-route/angular-route.js"></script>
    <script src="app.js"></script>
    <script src="controllers/login-controller.js"></script>
</head>
<body>
    <div ng-controller="LoginController as lg" >
        <div ng-if="lg.page != 'Login'" ng-include="'view/navigation-top.html'"></div>
    </div>

    <div ng-view></div>

</body>

login-controller.js login-controller.js

angular.module('myApp').controller("LoginController", LoginController);

function LoginController($http, $location, $scope) {
    var vm = this;
    vm.page = "Login";

    vm.login = function(){
        vm.page = "Main";
        $location.path('/')
    }
}

It is working when the vm.page is initiate and set to "Login", but the page does not change when i set it to "Main". 当启动vm.page并将其设置为“ Login”时,它正在工作,但是当我将其设置为“ Main”时,页面不会更改。

Appretiate if anyone can help me, thank you :D 不胜感激是否有人可以帮助我,谢谢:D

The problem is the $location.path('/') call in your Login-Function. 问题是您的登录功能中的$ location.path('/')调用。 This will navigate to /index.html and re-initialize the LoginController. 这将导航到/index.html并重新初始化LoginController。 Therefore the vm.page variable is reset to 'Login'. 因此,vm.page变量将重置为“登录”。

Remove the location.path() or create a service which reatains the Page/Login-State. 删除location.path()或创建一个包含页面/登录状态的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM