1

I have the following div:

<div style="font-size:15px;"  class="title">
    <div>{{specialOffer.Name}}</div>
</div>

I need to execute a JS function after {{specialOffer.Name}} is replaced with its value. Sorry, I'm an Angular newbie, trying to solve some bugs on an existing code.

I tried $(document).ready or window.onload, but these events get fired too quickly.

4 Answers 4

2

You can do that this way: Its one possibility and It is good to be inside directives while talking about rendering.

angular.module('myApp',[])
.directive('afterRender', function() {
  return {
    scope: {
      cb: '&'
    },
    link: function($scope, element, attrs) {
       var watch = $scope.$watch(function() {

      }, function() {
        // Runs after rendered
        $scope.$evalAsync(function() {
          $scope.cb();
        });
      });
    },
  };
})  
.controller('myCtrl',function($scope){
            
  $scope.specialOffer = {Name:"Woops!"}
  $scope.callMeAfterRender = function(){
     alert('Hey I am called :)');
  } 
        });
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div ng-app='myApp' ng-controller='myCtrl'>
<span after-render 
      cb='callMeAfterRender()'>{{specialOffer.Name}}</span>
</div>

Happy Helping!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you comment. I tried this, but when my function is called, the div is empty and looks like this: <div after-render="" cb="fitSpecialOfferTagText()" class="ng-isolate-scope ng-binding"> </div>
are you sure your two way binding is working ? {{specialOffer.Name}} is not empty ? the solution code i have given you is working all the time.
1

Try using ng-init, see this

<div style="font-size:15px;"  class="title">
    <div ng-init="functionName(specialOffer.Name)">
       {{specialOffer.Name}}
    </div>
</div>

Comments

0

Here's one way to do it.

$scope.$watch('specialOffer.Name', function(newVal, oldVal) {
  callYourFunction()
});

Every time specialOffer.Name changes, the callback is ran and the DOM is updated (meaning {{specialOffer.Name}} gets "filled in").


But this has a "code smell" to it. I can't be sure that it's a bad approach without knowing the context, but I'm pessimistic. Check out this to get a better understanding of how data binding works.

Comments

0

Here is the solution.Plese find the same. Here the problem was solved by using angularjs directive post link.

app.directive('afterRender', function () {
return {
    link: {
        post: function ($scope, elem, attr) {             
               $timeout(function () {
                  alert('Data was rendered'); 
               //  add your java script code               
              }, 0);
          }  
      }            
    };
});






<div ng-app='myApp' ng-controller='myCtrl'>
    <span after-render>{{expression}}</span>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.