Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何将一个控制器值更改为另一个控制器值_Angularjs_Service_Factory - Fatal编程技术网

Angularjs 如何将一个控制器值更改为另一个控制器值

Angularjs 如何将一个控制器值更改为另一个控制器值,angularjs,service,factory,Angularjs,Service,Factory,在此方法中,$scope.getBalAmount有一些值并绑定到index.html页面。在这里,索引页面充当母版页。我想在其他页面(在内容页面)上更改此值。是否可能。请提前感谢。在rootscope中设置 var getAgentBalanceAmount = function (ClientId, UserId) { $http({ method: 'GET', url: $scope.ip + '/getAgentBalanceAmount?Cli

在此方法中,$scope.getBalAmount有一些值并绑定到index.html页面。在这里,索引页面充当母版页。我想在其他页面(在内容页面)上更改此值。是否可能。请提前感谢。

在rootscope中设置

var getAgentBalanceAmount = function (ClientId, UserId) {
    $http({
        method: 'GET',
        url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
    })
    .then(function success(response) {
        $scope.getBalAmount = response.data[0].BalanceAmount;
    }, function error(response) {
        alert('Error');
    });
};

使用工厂。若要使用工厂,请在控制器中注入工厂(UserService)

要在index.html中获取金额值,请将工厂注入适当的控制器并使用get方法

  var getAgentBalanceAmount = function (ClientId, UserId) {
        $http({
            method: 'GET',
            url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
        })
        .then(function success(response) {
            $rootscope.getBalAmount = response.data[0].BalanceAmount;
        }, function error(response) {
            alert('Error');
        });
    };
JS

$scope.amount = UserService.getBalAmount();
工厂

var getAgentBalanceAmount = function (ClientId, UserId) {
  $http({
    method: 'GET',
    url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId;
  })
  .then(function success(response) {
    UserService.setBalAmount(response.data[0].BalanceAmount);
  }, function error(response) {
    alert('Error');
  });
};

在rootscope中或使用会话或本地存储方法进行设置:

app.factory("UserService", function() {
  var balAmount;
  return {
    getBalAmount: function() {
      return balAmount;
    },
    setBalAmount: function(amount) {
      balAmount = amount;
    }
  };
使用以下代码直接在会话存储中获取另一页:
console.log($window.sessionStorage.token)
访问所有页面

最好的方法是将
http.get()
代码提取到
factory\service
中,并在不同的控制器中随时随地使用它。
var getAgentBalanceAmount = function (ClientId, UserId) {
        $http({
            method: 'GET',
            url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
        })
        .then(function success(response) {
            $rootscope.getBalAmount = response.data[0].BalanceAmount;
        }, function error(response) {
            alert('Error');
        });
    };

var getAgentBalanceAmount = function (ClientId, UserId) {
        $http({
            method: 'GET',
            url: $scope.ip + '/getAgentBalanceAmount?ClientId=' + ClientId + '&AgentId=' + UserId + ''
        })
        .then(function success(response) {
           $window.sessionStorage.token=response.data[0].BalanceAmount;
        }, function error(response) {
            alert('Error');
        });
    };