简体   繁体   中英

AngularJS pass variable from custom directive to template

I made a custom directive:

js part:

angular.module("myDirectives", []).directive("ngShowbox", function(){
  return {
    restrict: "E",
    scope: {
      title: "="
    },
    template: "<a href='#' ng-click='show($event)'>{{title}}</a>",
    controller: function($scope, $element){
      $scope.show = function(event){
        // do something...
      }      
    }
  }
});

html part:

<ng-showbox title="whatToPutHere??"></ng-showbox>

I would like to pass some text from the title attribute, then my template will show the text. How can I make it?

Thank you very much :)

Use @ in directive scope:-

scope: {
      title: "@"
    },

Plunker for you :)

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