javascript
  • angularjs
  • directive
  • 2015-04-01 58 views 3 likes 
    3

    我做了一個自定義的指令:AngularJS傳遞變量從自定義指令模板

    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的一部分:

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

    我想通過從一些文字標題屬性, 然後我的模板將顯示文本。 我該怎麼做?

    非常感謝你:)

    回答

    5

    使用@在指令範圍: -

    scope: { 
         title: "@" 
        }, 
    

    Plunker你:)

    +1

    謝謝squiroid :) – kathy 2015-04-01 06:53:42

    +0

    很樂意幫助你@kathy – squiroid 2015-04-01 06:54:20

    +2

    剛一個音符,如果你將標題的值賦給一個控制器範圍值,那麼你需要使用'title:「=」'。 :),如果你只需要字符串值,而不是作爲'title'裏面的範圍值,你可以使用''title:「@」'' – 2015-04-01 06:58:29

    相關問題