javascript - Insert directive template after another element when this one is clicked -
i'm working on custom directive. have:
<button mydirective>click me</button>
my directive looks this:
return { restrict: 'ea', controller: 'controller', replace: false, template: '<h3>test</h3>', // more complex link: function( $scope, $element, $attrs ) { $element.on( 'click', function() { // code, shows template }); } };
result is:
<button mydirective>click me <h3>test</h3> </button>
but want have this:
<button mydirective>click me</button> <h3>test</h3> <!-- shown after button clicked -->
after button clicked template should shown after button.
thank you.
i suggest rendering button in directive template. here's example snippet:
angular.module('myapp', []) .directive('expander', function() { return { restrict: 'ea', replace: true, template: '<div><button ng-click="showcontent=true">{{buttontext}}</button><h3 ng-show="showcontent">test</h3></div>', link: function( $scope, $element, $attrs ) { $scope.buttontext = $attrs.text; } } });
<html ng-app="myapp"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script> </head> <body> <expander text="press me"></expander> </body> </html>
Comments
Post a Comment