如何将动态数据从一个模块控制器传递到另一模块控制器 [英] How to pass dynamic data from one module controller to another module controller

查看:46
本文介绍了如何将动态数据从一个模块控制器传递到另一模块控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AngularJS的新手,我想将动态值(用户名)从一个模块中的一个控制器传递到另一个模块中的另一个控制器.路由和其他工作正常.这是我的代码

I'm new to AngularJS, I want to pass dynamic value (username) from one controller in one module to another controller in another module. Routing and other things are working fine. This is my code

loginModule.js

loginModule.js

(function() {

      var app = angular.module("waleteros", ["ngRoute","ui.bootstrap","ngCookies"]);

        app.config(function($routeProvider) {
            $routeProvider
            .when("/",{
                templateUrl:"views/login.html",
                controller:"LoginCtrl"
            })
        }
    })

app.js

(function() {

      var app = angular.module("waleterosAdmin", ["ngRoute","ngGrid","ui.bootstrap","ngCookies"]);

      app.config(function($routeProvider) {
        $routeProvider
            .when("/home",{
                templateUrl:"homepage.html",
                controller:"HomeCtrl"
            })
        }
    })

loginCtrl.js

loginCtrl.js

(function(){

        var app = angular.module("waleteros");

        var LoginCtrl= function($scope,$location)
        {
            $scope.signIn=function(email,pin)
            {
               //Some authentication code...
               //Here i want to pass the username to homectrl.js
               window.location.href="views/home.html"
            }
        }
        app.controller("LoginCtrl", LoginCtrl);
    })

HomeCtrl.js

HomeCtrl.js

 (function(){

        var app = angular.module("waleterosAdmin");

        var HomeCtrl=function($scope)
        {
             //Here i want to get the username
        }

        app.controller("HomeCtrl", HomeCtrl);
    })

推荐答案

关于服务的警告一语是它们在应用程序的生存期内一直存在,即它们仅被实例化一次.我遇到了各种情况,特别是在实施路由后,我想从服务中删除数据以节省空间并用新数据替换(您不想每次查看一个服务时就一直添加到该服务中)不同的观点).为此,您可以编写一个擦除"方法,以在路由更改时清理服务,或者将数据粘贴到指令(在其控制器上),将控制器放入自己的指令中,并具有这些要求第一条指令,这样就可以从控制器访问数据,并且具有在DOM元素声明为destroy(例如,更改视图)后就可以擦除数据的额外好处.

One word of warning about services is that they persist for the lifetime of the application, i.e. they are only instantiated once. I have run into scenarios, especially once routing is implemented, where I want to wipe the data off of the service to save space and replace it with new data (you don't want to keep adding to this service every time you look at a different view). To do this, you could either write a "wipe" method that you call to clean the service on the route changes, or stick the data into a directive (on its controller), put the controllers into their own directives, and have these require the first directive, so that the data is accesible from the controller's with the added benefit of being wiped once the DOM element is declared on is destroy (on a view change, for instance).

这篇关于如何将动态数据从一个模块控制器传递到另一模块控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆