简体   繁体   中英

how to get param in my directive

I write template for my directive, template.html:

<ul><span>{{name}}</span>
   <li ng-repeat="value in values">
</ul>

Plug directive like:

app.directive('myDir', function(){
   return{
    restrict: 'A',
    templateUrl: 'template.html'
   };
});

I have some js object looks like:

const myObj:{
    name:"MyObjName",
    values:["val1","val2","val3","val4"]
}

Tell me please how can I send to my template my object so it will render directive like I want?? In index html it looks like <div my-dir="{{myObj}}"></div>

You can pass the object to directives scope and it'll be available in your template. Just use

 app.directive('myDir', function(){
   return{
    restrict: 'A',
    scope: {
       'myDir': '='
    },
    templateUrl: 'template.html'
   };
});

and then you can pass the object the way you want to the directive.

<div my-dir="myObj"></div>

Hope this will help.

app.directive('myDir', function(){
   return{
    scope: {
      myDir: "="
    },
    restrict: 'A',
    templateUrl: 'template.html'
   };
});

<ul><span>{{myDir.name}}</span>
   <li ng-repeat="value in myDir.values"></li>
</ul>

Usage:

<div my-dir="myObj"></div>

in class declare

public myObj: any;

In constructor set

this.myObj={
    name:"MyObjName",
    values:["val1","val2","val3","val4"]
};

In template

<div [my-dir]="myObj"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM