简体   繁体   English

AngularJS将变量从自定义指令传递给模板

[英]AngularJS pass variable from custom directive to template

I made a custom directive: 我做了一个自定义指令:

js part: js部分:

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: HTML部分:

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

I would like to pass some text from the title attribute, then my template will show the text. 我想从title属性传递一些文本,然后我的模板将显示文本。 How can I make it? 我该怎么做?

Thank you very much :) 非常感谢你 :)

Use @ in directive scope:- 在指令范围内使用@: -

scope: {
      title: "@"
    },

Plunker for you :) Plunker为你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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