简体   繁体   English

如何将$ scope中的值传递给AngularJS中的内部控制器?

[英]How can I pass down a value in the $scope to an inner controller in AngularJS?

In my angular app.js that's the main controller in my index.html I want to have a function like this: 在我的angular app.js中,我的index.html中的主控制器我希望有这样的函数:

    $scope.login = function (user) {
        $scope.authenticating = true;
        var config = {
            method: 'POST',
            url: '/api/Account/Login',
            data: {
                'userName': user.loginUserName,
                'password': user.loginPassword,
                'rememberMe': user.loginRememberMe
            }
        };
        $http(config)
            .success(function (data) {         
                authentication.isAuthenticated = true;
                authentication.userName = user.loginUserName;
                localStorage.isAuthenticated = 'yes';
                $scope.template = $scope.templates[1];
                $state.transitionTo('home');
            })
    };

In my controllers that are for templates inside index.html I want to be able to get access to the userName. 在我的index.html中的模板控制器中,我希望能够访问userName。 Can someone tell me how I can do this? 谁能告诉我怎么做到这一点? Initially I tried setting $scope.user = {} in the app.js and then setting $scope.user.userName but when I open another template and its controller that's inside app.js the $scope.user does not seem to be visible. 最初我尝试在app.js中设置$ scope.user = {}然后设置$ scope.user.userName但是当我打开另一个模板及其app.js 内的控制器时,$ scope.user似乎不可见。

You could use a service to share data between controllers 您可以使用服务在控制器之间共享数据

app.service('userService', function() {
  var user = {};

  setUserName = function(name) {
    user.name = name;
  };

  getUser = function(){
      return user;
  };
});

Controller that sets data 设置数据的控制器

app.controller('FooController', ['$scope', 'userService',
    function ($scope, userService) {
        $scope.login = function (user) {
            $scope.authenticating = true;
            var config = {
                method: 'POST',
                url: '/api/Account/Login',
                data: {
                    'userName': user.loginUserName,
                    'password': user.loginPassword,
                    'rememberMe': user.loginRememberMe
                }
            };
            $http(config)
                .success(function (data) {
                    authentication.isAuthenticated = true;
                    userService.setUserName(user.loginUserName);
                    localStorage.isAuthenticated = 'yes';
                    $scope.template = $scope.templates[1];
                    $state.transitionTo('home');
                })
        };
    }
]);

Other Controller, that retrieves data 其他控制器,用于检索数据

app.controller('BarController', ['$scope', 'userService', function($scope, userService) {
    var username = userService.getUser().name
});

Since your retrieval is async, you might need to add some event/callback mechanism to your user service, so the controllers get notified when the name is set / changes. 由于您的检索是异步的,您可能需要向用户服务添加一些事件/回调机制,以便在设置/更改名称时通知控制器。

All child controllers inherit $scope variables from parents 所有子控制器都从父级继承$scope变量

<div ng-controller="c1">
    <div ng-controller="c2"></div>
</div>

youApp.controller('c1', ['$scope', function($scope){
    $scope.blabla = 1;
}]);

youApp.controller('c2', ['$scope', function($scope){
    // $scope.blabla is also 1 here because c2 is a child of c1
}]);

You can store anydata in built-in $rootScope service. 您可以在内置$rootScope服务中存储任何数据。 Services are singleton in Angular 服务是Angular中的单身人士

yourApp.run(['$rootScope', functin($rootScope){
    $rootScope.foo = 'bar';
    // you can inject $rootScope in any controller you want and use $rootScope.foo
}]);

As @Robin suggest, I also recommend to write your own service for authorization 正如@Robin建议的那样,我还建议您编写自己的服务以进行授权

暂无
暂无

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

相关问题 Angularjs我可以从另一个控制器更改控制器的$ scope值吗? - Angularjs Can I change $scope value of a controller from another controller? 将范围值从指令传递到控制器-AngularJS - Pass scope value from Directive to Controller - AngularJS 我如何将范围从控制器传递到angularjs中的服务? - how do i pass scope from controller to service in angularjs? AngularJS:如何根据控制器范围更改ng类三元运算符的值? - AngularJS:How can i change value of the ng-class ternary operator as per the controller scope? 如何将指令的作用域值作为$ scope传递给控制器 - How to pass scope value of directive into controller as $scope AngularJS - 我如何使用 $scope 在控制器中调用函数 - AngularJS - how can i call a function in a controller with $scope 如何将数据从模态内部控制器传递到AngularJs中的外部控制器 - How to pass data from inner controller of modal to outer controller in AngularJs 如何在AngularJS中将控制器的传递值返回到指令的模板属性? - How can I get pass value from controller back to directive's template attribute in AngularJS? 我可以将$ scope变量从一个函数传递到控制器内的另一个函数吗? AngularJS - Can I pass a $scope variable from one function to another function inside the controller? AngularJS 是否可以在Angularjs控制器的json对象中传递$ scope.value? - Is it possible to pass $scope.value inside json object in Angularjs controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM