【问题标题】:get value from another controller angular js从另一个控制器角度js获取价值
【发布时间】:2017-11-28 15:55:01
【问题描述】:

我有问题。我想将 get 从 rootScope.reslogin2 传递给 scope.user 但它不会显示在这里是我的 js 文件

    app.controller("logincont", ['$scope','$http','md5','$window','$rootScope',function($scope,$http,md5,$window,$rootScope){
     $scope.$watch('pass', function(val) {
          $scope.password = md5.createHash($scope.pass || '');
        });
    $scope.cari = function () {
            $http.get('http://localhost:8089/MonitoringAPI/webresources/login?a='+$scope.userid+'&d='+$scope.password).then(function(response){
           $scope.reslogin  = response.data;
           $rootScope.reslogin2 = response.data[0].username;
           $scope.reslogin3 = response.data[0].lsmenu[0].idmenu; 
           console.log($scope.reslogin);
           console.log($scope.reslogin2);
      });
          };   

          }]);


    app.controller("moncont", ['$scope','$http','$filter','$rootScope',function($scope,$http,$filter,$rootScope){
      $scope.user = $rootScope.reslogin2;
      console.log($scope.user);
}]);

我想在这一段显示<p>Username : {{user}}</p>

【问题讨论】:

  • 你在控制台中得到了什么?
  • 未定义@Sunil Lama
  • 在下面找到答案,看看是否有帮助
  • andika 可能根本没有分配来自 api 的数据。你能看到$scope.cari函数记录了什么吗?
  • $scope.cari 用于 ng-click。所以当按钮点击时,它会执行 $scope.cari

标签: javascript angularjs rootscope


【解决方案1】:

确保在访问之前分配了值。 在 moncont 控制器中,将 $rootScope.reslogin2 保留在 watch 中,并在 watch 函数中将其分配给 $scope.user。

【讨论】:

    【解决方案2】:
    来自 moncont 控制器的

    user 与所需值不同步,因此要修复它,例如,您可以通过 Object.defineProperty 声明相应的 getter,而不是 $scope.user = $rootScope.reslogin2;

    angular.module('app', [])
      .controller('logincont', function($rootScope, $scope, $timeout) {
        $scope.cari = function() {
          $timeout(function() {
            $rootScope.reslogin2 = ($rootScope.reslogin2 || 0) + 1;
          }, 1000)
        }
      })
      .controller('moncont', function($rootScope, $scope) {
        Object.defineProperty($scope, "user", {
          get: function() {
            return $rootScope.reslogin2
          }
        });
      })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
    </script>
    
    <div ng-app='app'>
      <div ng-controller='logincont'>
        <button ng-click='cari()'>Cari</button>
      </div>
      <div ng-controller='moncont'>
        user: {{user}}
      </div>
    </div>

    【讨论】:

      【解决方案3】:

      在您的控制器中添加以下方法。

      $scope.init = function(){
         $scope.user = $rootScope.reslogin2;
         console.log($scope.user);
      }
      

      您的模板应如下所示:

      <div ng-controller="moncont" ng-init="init()">
          <p>Username : {{user}}</p>
      </div>
      

      【讨论】:

      • 你能不能给点麻烦或小提琴? @Syedur
      • 我已经更新了代码。你能试试这个吗?
      • 我试过了,但还是未定义
      • 不是应该使用ng-init
      猜你喜欢
      • 2014-01-03
      • 2018-10-06
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      相关资源
      最近更新 更多