angularjs - Angular digest is not running when a promise is resolved -


in angular application have directive isolated scope. directive gets string parameter in scope , since value might initialized parent scope in asynchronous process - gets promise that's resolved parent when value ready use.

strange thing: when promise resolved value in directive's scope still undefined, although in parent controller has value.

see plunker example of experience: https://plnkr.co/edit/wlilkao95xwwu2rf29ox?p=preview

in controller:

  $scope.defer = $q.defer();    // simulate async operation   $timeout(function(){      $scope.ctrlvalue = 'hi';        $scope.defer.resolve();   }); 

in directive:

 scope.valuepromise.then(function(){            alert(scope.value);  }); 

important note: if wrap 'alert' line of code in block of $timeout - scope.value has right value. seems angular not running digest when promise resolved.

any ideas?

use $timeout in directive. time required apply changes of parent scope directive scope. tried value while not updated.

var app = angular.module('app', []);    app.controller('ctrl', function($scope, $q, $timeout) {    $scope.defer = $q.defer();        $timeout(function() {      $scope.ctrlvalue = 'hi';      $scope.defer.resolve();    });  });    app.directive('dir', function($timeout){    return {      restrict: 'e',      scope: {        value: '=',        valuepromise: '='          },      link: function(scope) {        scope.valuepromise.then(function() {          $timeout(function() {            alert(scope.value);                  });        });      }    };  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <div ng-app="app">            <div ng-controller="ctrl">              <h1>hello plunker!</h1>        <dir value="ctrlvalue" value-promise="defer.promise"></dir>      </div>    </div>


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -