1

As the title says, I would like to call the button on a HTML page as a custom directive. Like:

<buy-button></buy-button>

Below is the code I want to transform:

<md-button class="md-cornered" 
           ng-disabled="0>=gains" 
           ng-click="buyTaxi()">Buy a Taxi</md-button>

1 Answer 1

1

This is a basic transform of your element into a directive. There is no real advantage of doing it but it does what you want:

> demo fiddle

View

<div ng-controller="MyCtrl">
  <my-directive></my-directive>
</div>

AngularJS application

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {});

myApp.directive('myDirective', function () {
    return {
      restrict: 'E',
      replace: true,
      template: `<md-button class="md-cornered" ng-disabled="0>=gains" ng-click="buyTaxi()">Buy a Taxi</md-button>`,
      link: function (scope, element, attrs) {

         scope.gains = 1;

         scope.buyTaxi = function () {
            console.log('Buy me a nice taxi!');
         }
      }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot! Well, the advantage is seen in a cleaner html code and a more structured app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.