Angularjs - 如何根据响应数据元素之一设置全局变量的值

Angularjs - How to set value of Global variable according to one of the response data element

我想根据函数hello中的定义来设置glovalvar的值 我正在获取响应数据并据此设置 globalvar 值。 这是片段:

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  var globalvar;
  $scope.globalvar='';
  $scope.hello = function()
  {
    //here I want to set the value of glovalvar.I am getting response              data & according to that I want to set globalvar Value.
      //For an example I am creating this funtion.
    //How can I achieve this.
    //I tries below code:
    $scope.globalvar="ee";
  }
  console.log($scope.globalvar)
});
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

    </head>
  <body ng-app="myApp" ng-controller="myCtrl">
    {{globalvar}}
    </body>
  </html>

您应该在 console.log($scope.globalvar)

之前调用函数 $scope.hello()
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  var globalvar;
  $scope.globalvar='';
  $scope.hello = function()
  {
    //here I want to set the value of glovalvar.
    //How can I achieve this.
    //I tries below code:
    $scope.globalvar="ee";
  }
  $scope.hello()
  console.log($scope.globalvar)
});