1

I've a simple directive

'use strict';

angular.module('ludaooApp')
  .directive('rating', function () {
    return {
      templateUrl: 'app/rating/rating.html',
      restrict: 'EA',
      link: function (scope, element, attrs) {
          scope.mark = attrs.mark;
          console.log(attrs);
    };
  })

This directive only log the attributes of the directive.

Here is how I use that directive :

<rating mark="{{game.rating}}" style="font-size: 30px"></rating>

And here is the result of the web inspector :

enter image description here

As you can see, mark is empty mark="" on the first line. But after, it is filled in with its value mark="4.16".

The result is that if I console.log(scope.mark), I see 0 instead of 4.16.

I think it's because the 4.16 is retrieved from the database and, the code is executed before that {{game.rating}} is initialized in the controller.

So the question is, how to deal with this problem ? And how to access to the "4.16" ?

3
  • 0 is the value of game.rating at the time of rendering the directive.But the value you find in dev console is the current value of mark evaluated after logging in the console. If you hover near the i you will find that message. Commented Jan 27, 2016 at 17:20
  • It's because you forgot to add scope: { mark: '=' } to your directive. Add it after restrict: 'EA' (although order doesn't matter). Commented Jan 27, 2016 at 18:14
  • you can use a $watch on the mark and check if value is greater than 0 and then do something like console.log(value) Commented Jan 27, 2016 at 19:19

3 Answers 3

2

Try to use angular promises in service, that receives data from api, and pass the state of deffered object to ng-if - this decision can help to set aside rendering of rating directive.

Simple example:

<div ng-app="ludaooApp">
  <div ng-controller="GameCtrl as game">
    <rating mark="{{game.rating}}" ng-if="game.$promise.status"></rating>
  </div>
</div>


 (function(angular){
   "use strict";

   angular.module('ludaooApp',[])
   .controller('GameCtrl', ['$timeout', '$q', function($timeout, $q){
        var game = this;
        var deferred = $q.defer()
        game.$promise = deferred.promise.$$state
        $timeout(function(){
            game.rating = 4.16;
            deferred.resolve();
        },3);    
    }])
    .directive('rating', function () {
       return {     
          restrict: 'EA',      
          link: function (scope, element, attrs) {
             scope.mark = scope.$eval(attrs.mark);
             console.log(attrs);
       }
      };
    })
 })(window.angular);

JSFiddle

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

Comments

1

I think Enver's solution may work but it's over-complicated. The issue is caused by game.rating has not been completely bound when it's read. A more simple solution is to wait until binding of game.rating is completely done and assigned to mark attribute, as follows:

link: function (scope, element, attrs) {
    attrs.$observe("mark", function(value) {
        // read "mark" here
        console.log(attrs.mark);
    });
};

Comments

0

I believe your directive is getting created before your {{game.rating}} has completed binding.

Try using ngAttr so that your attribute is defined only after the binding completes:

<rating ng-attr-mark="{{game.rating}}" style="font-size: 30px"></rating>

1 Comment

Have the same issue, got undefined this way.

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.