0

My view is simple:

<job-template ng-if="job"></job-template>

My directive:

.directive('jobTemplate', function(){
  return {
    restrict: 'AE',
    replace: true,
    link: function(scope,element, attrs) {
        var JobStatus = scope.job.JobStatus;
        var text = "<p>"; 

        if(JobStatus === 'Created'){
          text += "{{job.id}} was created."
        }

        else if(JobStatus === 'Processing'){
          text += "{{job.id}} is currently processing."
        }

        text += "</p>";
        console.log("text");
        console.log(text);
        return text;
    }
  };

})

When I run my page, my <job-template> element isn't replaced with anything - although the correct text is loaded to the console.

What have I done wrong here?

2 Answers 2

3

link function isn't meant to used for returning a HTML as you are thinking, It is basically meant to be there, to provide control over angular compiled DOM, when scope is linked to directive element. You could have html over template/templateUrl option in directive. Use ng-if for having conditional element.

Directive

.directive('jobTemplate', function() {
  return {
    restrict: 'AE',
    replace: true,
    template: '<p>'+
        '<span ng-if="job.JobStatus == \'Created\'">{{job.id}} was created.</span>'+
        '<span ng-if="job.JobStatus == \'Processing\'">{{job.id}} is currently processing.</span>'+
      '</p>',
    link: function(scope, element, attrs) {}
  };

})

Demo here

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

Comments

0

Try this code https://plnkr.co/edit/cpdAqWN0SeUx5Gy9CQkR?p=preview

app.directive('jobTemplate', function($compile){
  return {
restrict: 'AE',
replace: true,
link: function(scope,element, attrs) {
  console.log(element)
    var JobStatus = scope.job.JobStatus;
    var text = "<p>"; 

    if(JobStatus === 'Created'){
      text += "{{job.id}} was created."
    }

    else if(JobStatus === 'Processing'){
      text += "{{job.id}} is currently processing."
    }

    text += "</p>";
    element.html(text);
    $compile(element.contents())(scope);
 }
};
})

5 Comments

the way you are implemented is correct..but you should left conditional adding or removing part of DOM should using ng-if, so that wouldn't make a problem if job value will loaded by an ajax.
We can do this too within directive. Previously i made with the wordpress SPA. It's too much long directive so not possible to mention those all things but i managed by using scope.$watch('$viewContentLoaded', function(){}) within directive.
$watch on $viewContentLoaded loaded wouldn't work too..you need to specifically place a watch over job, but that would be an bad practice to have $watch in code.. Try to avoid watchers so that code will be more cleaner..
Yup agree with you. Sometime different environment and technology don't allow than this. In my case wordpress is totally useless but client choose that only technology and he never left option for me.
that's why I said you are correct.but that will only work with static data as you mentioned..

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.