在AngularJS中的另一个控制器中更新变量

时间:2021-08-28 23:22:44

okay, so I am building a web-app using angular.js. I have started to implement user authentication.

好的,我正在用angular.js构建一个web应用程序。我已经开始实现用户身份验证。

I did this with it's own controller:

我是用它自己的控制器做的

app.controller("userControl", ["$http", "share", function($http, share){
this.login = {};
var user = this;
this.doLogin = function(){
    this.login.fn = "login";
    $http.post('classes/main.php', user.login).success(function(data){
        console.log(data.Error);
        if(data.Error == undefined){
            alert("Success");
            share.user = data;
            window.location.href = "#memberHome";
        }
    }); 
}
}]);

and I have a member's page with it's own controller:

我有一个成员的页面有它自己的控制器:

HTML:

HTML:

<div id="memberHome" data-role="page" ng-controller="member-Ctrl as member">
    <nav class="navbar navbar-inverse">
        <div class="container-fluid">
            <div class="navbar-header">
                <p class="navbar-brand" href="#">Calendar</p>
            </div>
            <ul class="nav navbar-nav navbar-left">
                <li><a href="#">Overview</a></li>
                <li><a href="#">Friends</a></li>
            </ul>

            <ul class="nav navbar-nav navbar-right">
                <li><a href="#"><span class="glyphicon glyphicon-user"></span> Logged in as: {{member.user.forename + " " + member.user.surname}}</a></li>
                <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Log Out</a></li>
            </ul>
        </div>  
    </nav>
    <div data-role="content">
        <calendar selected="day"></calendar>

    </div>
</div>

javascript:

javascript:

app.controller("member-Ctrl", ["share", function(share){
this.user=share.user;

}]);

I need the User controller to tell the member controller the details of the new user.

我需要用户控制器告诉成员控制器新用户的详细信息。

I googled this and looked at the answers of a similar question, which led me to This

我在谷歌上搜索了一下,然后查看了一个类似问题的答案,这让我想到了这个问题

and i created a service as shown in the video:

我在视频中创建了一个服务

app.service("share", function(){
    this.user = {}

});

Now whenever i login and get redirected to the memberHome page, it tells me Logged in as:

现在,每当我登录并被重定向到memberHome页面时,它会告诉我登录为:

Can anybody see why this might be?

有人知道这是为什么吗?

I am very new to Angular, and am learning, just about, everything when I need it.

我对角度很陌生,我正在学习,当我需要的时候。

I should probably say that i am using jQuery Mobile (for a multi-page document) and Bootstrap 3

我应该说,我正在使用jQuery Mobile(用于多页文档)和Bootstrap 3

1 个解决方案

#1


10  

You can use a service to share information between controllers or use the $rootScope

您可以使用服务在控制器之间共享信息,或者使用$rootScope

var app = angular.module('mymodule',[]);
app.controller('Controller1', ['$scope','$rootScope',
  function($scope, $rootScope) {
    $rootScope.showImages = true;
}]);

app.controller('Controller2', ['$scope','$rootScope',
  function($scope, $rootScope) {
    $rootScope.showImages = false;
}]);

And in the template:

和模板:

<div ng-controller="Controller1">
    <div class="images" ng-show="$root.showImages"></div>
</div>

#1


10  

You can use a service to share information between controllers or use the $rootScope

您可以使用服务在控制器之间共享信息,或者使用$rootScope

var app = angular.module('mymodule',[]);
app.controller('Controller1', ['$scope','$rootScope',
  function($scope, $rootScope) {
    $rootScope.showImages = true;
}]);

app.controller('Controller2', ['$scope','$rootScope',
  function($scope, $rootScope) {
    $rootScope.showImages = false;
}]);

And in the template:

和模板:

<div ng-controller="Controller1">
    <div class="images" ng-show="$root.showImages"></div>
</div>