简体   繁体   English

AngularJS中带有ui-view的根控制器的继承变量

[英]Inheritance variable from root controller with ui-view in AngularJS

My application does a http call, the result is stored in a variable in the scope. 我的应用程序执行http调用,结果存储在范围内的变量中。

angular.module('simfraAppController', [])
.controller('simfraController', ['$scope', 'RetrieveInformation', 'RetrieveTaxInformation', 'ErrorModal',
    function ($scope,RetrieveInformation, RetrieveTaxInformation, ErrorModal) {
        $scope.formData = {};

        RetrieveInformation.then(function success(response) {
            $scope.personalData = response.data.data;

        }, function error(response) {
            ErrorModal();
        });

        RetrieveTaxInformation.then(function success(response) {
            $scope.taxData = response.data.data;

        }, function error(response) {
            ErrorModal();
        });
    }
]);

.state('landing', {
    url: "/",
    templateUrl: "simfra/dashboard.html"
})

In this controller is the call made and sto in the variable. 在此控制器中进行调用并存储在变量中。

This is my other controller 这是我的另一个控制器

.controller('SimfraDirectDebitApplicationController', ['$scope',    '$rootScope', '$state', 'Command', 'uuid4',
    function ($scope, $rootScope, $state, Command, uuid4) {

    console.log($scope.personalData);
});

.state('direct_debit_application', {
                url: "/incassoaanvraag",
                templateUrl: "simfra/direct-debit-application/inquiry.html"
 })

Ofcourse i have nested the html. 当然我已经嵌套了html。

<body data-ng-controller="simfraController">
    <div data-ng-controller="SimfraDirectDebitApplicationController">
    </div>
</div>

My problem is when i init the application trough the default route "/" and i navigate to the route "direct_debit_application" it all works great. 我的问题是,当我通过默认路由“ /”初始化应用程序并且导航至路由“ direct_debit_application”时,它都很好用。

BUT, when i reload the page, the request is still being made (as far as i can see in the console) but the variable is 'undefined' 但是,当我重新加载页面时,仍在发出请求(据我在控制台中看到的),但变量为“未定义”

How can i get this thing to work properly? 我怎样才能使这东西正常工作?

This worked for me 这对我有用

set the value in First controller using following code 使用以下代码在“ First controller”中设置值

$scope.$emit('key', {data: value}); //to set the value

Get the value in second controller using follwing code 使用以下代码在第二个控制器中获取值

 $rootScope.$on('key', function (event, data) {}); //to get the value

or you can try getter setter methods too 或者您也可以尝试使用getter setter方法

I know why its not working!! 我知道为什么它不起作用!

The $http request is still being made and the DOM has been loaded. $ http请求仍在进行中,并且DOM已加载。 So as result the variable is empty. 因此,该变量为空。

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

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