1

I have simple directive and I want access to the variable in link function in the template. how i can achieve that?

My directive :

app.directive("hello", function() {
  return {
    restrict: "E",
    template: '<div> '+str+' <div/>'
    link : function(scope, element, attrs) {
      var str = "hello";
    }
  }
});

Here is a code on codepen: demo

3 Answers 3

2

add the variable in the scope and they will be available in the template

scope.str = "hello";

and your template should use the angular expression

template: '<div>{{str}}<div/>',

so your directive will be like

app.directive("hello", function() {
  return {
    restrict: "E",
    template: '<div>{{str}}<div/>',
    link : function(scope, element, attrs) {
      scope.str = "hello";
    }
  }
});

EDIT

If you want to bind html use ngbindHtml

Please find the plnkr

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

3 Comments

It's work but instead of use '<div> '+str+' <div/>' i use '<div> {{str}} <div/>'
hi i have similar situation but instead of string i need to pass an html string <h1>hi<h1/> , how can i achieve that , i f you can please update your plnkr. Thanks in advance
@Aji Please look at the link i.e. ngBindHtml. At this page you will get the exact example of what you are looking for
1

there are multiple ways i need to explain you here so please consider both side changes need i mean you want to send variable from directive call

<html ng-app="app">
<body>
  <hello data-str="'I am don'"></hello>
</body>
</html>

here data-str means there str is variable which having text "I am don" thats it

now

angular.module("app", []);

angular.module("app").directive("hello", function() {
  return {
    scope:{
      str:"="
    },
    restrict: "E",
    template: '<div>{{linkVar}}{{str}}<div/>',
    link : function(scope, element, attrs) {
     scope.linkVar = 'It works!' 
      console.log(scope.str);
    }
  }
});

Here

you can see this in directive object added by me

 scope:{
          str:"="
        },  

here we decide that "str" will be provided when directive calls in html like this instead of only this Please take care

now

scope.linkVar = 'It works!' 

this is most important thing you should see scope.linkVar means you had just javascript variable with name str = "hello"; this is not angular way so angular will not update its all references. I mean when you use in templates.

now hope it clear and let me know if yes. Have a great time

khajaamin

Comments

0

You can use 'this'

app.directive("hello", function() {
  return {
    this.str = '';
    restrict: "E",
    template: '<div> '+this.str+' <div/>'
    link : function(scope, element, attrs) {
      this.str = "hello";
    }
  }
});

1 Comment

When I use this keyword print undefined instead of hello !

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.