angularjs - Angular Directive not updating element using interval -


i have directive

angular.module('mydirectives').directive('slideshow', function ($interval) { return{     scope:{slideshow:'=slideshow'},     link:function(scope, element, attrs){                   element.css("background-size","cover");         element.css("background-repeat","none");         element.css("background-position","center center");         element.css("background-blend-mode","color");         element.css("background-color","rgba(0, 0, 0, 0.5)");          scope.index=0;          function nextslide()         {             if(!scope.slideshow) return;             if(scope.slideshow.sources.length===0) return;              var url=scope.slideshow.sources[scope.index++];             if(scope.index>=scope.slideshow.sources.length) scope.index=0;              element.css({'background-image': 'url(' + url +')'});         }          nextslide();         var interval= $interval(nextslide,3000)         scope.$on("$destroy",function(){             $interval.cancel(interval);         })     } } }); 

this how apply

<section class="primary" slide-show="slideshow"> 

now controller provides property "slideshow" gets value via http request. when comes response sets value of slideshow this

$scope.slideshow={sources:["http:\\sources\someimage.jgp"]}   webapi.gethomepagemodel().then(function(model){    $scope.model=model;    $scope.slideshow=model.slideshow;         },function(error){    console.dir(error); }); 

the problem: when runs default value of slideshow works , element's background-image set after response http new value set slideshow when interval function "nextslide" executes background-image not updated. in debugger can see url values being picked correctly element not updated.

edit:i making stupid mistake, updated model not expected elements in sources not strings expected (they being generated complex objects rather string value.) working now. no need scope.$applyasync because $interval service handles you

if using setinterval need manually rerun angular's digetst cycle:

function nextslide()     {         if(!scope.slideshow) return;         if(scope.slideshow.sources.length===0) return;          var url=scope.slideshow.sources[scope.index++];         if(scope.index>=scope.slideshow.sources.length) scope.index=0;          element.css({'background-image': 'url(' + url +')'});          scope.applyasync();  //this line!         //may not work in older angular versions, if such should use scope.apply()      } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -