簡體   English   中英

將rootScope值從一個控制器傳遞到另一個控制器

[英]Passing rootScope value from one controller to another controller

我試圖使用$ rootscope將值從控制器傳遞給另一個,我首先要做的是像這樣初始化rootScope

app.run(function ($rootScope) {
    $rootScope.emp=0 ;
});

然后我在這樣的一個控制器中更改rootScope的值

app.controller("datatableController", function ($scope, DTOptionsBuilder, $http, $rootScope) {
 $scope.update = function (data) {
        alert(data);
        $scope.message = $http.post('http://localhost:5000/api/employee/view', data).
            then(function (response) {
                $scope.userEdit = response.data;
                $rootScope.emp=$scope.userEdit[0].id;
                alert($rootScope.emp);
            });
        window.location.href = "updateEmployee.html";

    };
});

然后我試圖在我的第二個控制器中訪問此更改后的$ rootScope.emp值,但獲取我在初始化時設置的值為0

app.controller("UpdateController", function ($scope, $http, $rootScope) {

    $scope.userid=$rootScope.emp;
    alert($scope.userid);
});

嘗試這個 :

app.controller("datatableController", function ($scope, DTOptionsBuilder, $http, $rootScope) {
 $scope.update = function (data) {
    alert(data);
    $scope.message = $http.post('http://localhost:5000/api/employee/view', data).
        then(function (response) {
            $scope.userEdit = response.data;
            $rootScope.$apply(function(){
                $rootScope.emp=$scope.userEdit[0].id;
            });
            alert($rootScope.emp);
        });
    window.location.href = "updateEmployee.html";

};
});

詳細答案可以在這里找到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM