简体   繁体   English

AngularJS 1.2.26 $ scope异步函数执行摘要循环错误

[英]AngularJS 1.2.26 $scope asynchronous function performs digest cycle error

I need to have a function in the scope which result will change asynchronously. 我需要在范围内有一个函数,其结果将异步更改。

It's mandatory to have a function to be used as an expression , so I cannot use a single property. 必须将一个函数用作表达式 ,所以我不能使用单个属性。

In my example, the function returns a property of an object which will be modified asynchronously. 在我的示例中,该函数返回将被异步修改的对象的属性。 This performs a digest error although the value is equal over the whole digest cycle. 尽管在整个摘要周期中该值相等,但这会执行摘要错误。 Here the example: http://plnkr.co/edit/YmyroMiMyMzUaLW4tc7V (Caution: it could hang your browser) 示例如下: http : //plnkr.co/edit/YmyroMiMyMzUaLW4tc7V (警告:它可能会挂起浏览器)

myApp.controller('Ctrl1', ['$scope', '$http',  function($scope, $http) {
    var myObj = {found:false};
    $scope.util = {};

    $scope.util.asyncFunc = function(){

      $http.get('http://localhost').then(changeValue,changeValue);

      return myObj.found;
    }

    function changeValue(){
      myObj.found = true;
    }
}]);

Any idea how to solve it? 知道如何解决吗?

I have a solution, the problem was that executing all the time $http.get, it creates promises which will be resolved at the same time. 我有一个解决方案,问题是一直执行$ http.get,它创建了将在同一时间解决的promise。 So, first I solve the problem by checking if the promise is already created. 因此,首先,我通过检查是否已经创建了承诺来解决问题。 Afterwards in the callback of the promise I set my var which controls if teh promise is created to null, but I redefine the function doing a kind of bridge which returns the same value, to avoid executing $http.get in the same $digest: http://plnkr.co/edit/LOXDzVC4do2GTRtyNgSU 然后,在promise的回调中,将我的var设置为控制是否将创建promise创建为null,但是我重新定义了函数,使之返回了相同的值,以避免在同一$ digest中执行$ http.get: http://plnkr.co/edit/LOXDzVC4do2GTRtyNgSU

var myApp = angular.module('myApp', []);
//nueva 2015-06-09
myApp.controller('Ctrl1', ['$scope', '$http', '$q', function($scope, $http, $q) {
    var myObj = {found:false};
    $scope.util = {};
     var pro;
    $scope.util.asyncFunc = doGet;



    function doGet(param1, param2){
      if(!pro){
        pro = $http.get('http://localhost').then(changeValue,changeValue);
      }

      return myObj.found;

      function changeValue(){
        if(param1 !== "2"){
          myObj.found = true;
        }else{
          myObj.found = false;
        }


        $scope.util.asyncFunc = function(param1, param2){
          $scope.util.asyncFunc = doGet;
          pro = null;

          return myObj.found;
        }
    }
    }
}]);

and the html: 和html:

<body>
    <div ng-controller="Ctrl1">
          <br/><br/>
          Param1:<input type="text" ng-model="model.param1" />
          Param2:<input type="text" ng-model="model.param2" />

        AsyncFunc: {{util.asyncFunc(model.param1, model.param2)}}
    </div>

    <script src="http://code.angularjs.org/1.2.26/angular.js"></script>
    <script src="script.js"></script>
  </body>

Well, I think it's surely not the best solution, if you have other approach it would be very helpful. 好吧,我认为这肯定不是最佳解决方案,如果您采用其他方法,则将非常有帮助。

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

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