Showing posts with label directive. Show all posts
Showing posts with label directive. Show all posts

angularjs controller in directive

app.directive('customButton', function() {
  return {
    restrict: 'A',
    scope: true,
    template: '<button ng-click="clickMe()" type="button">Click Me</button>',
    replace: true,
    controller: function($scope, $element) {
      $scope.clickMe = function() {
         alert("yes Its working"); 
      }
    }
  }
});

Use this directive in html page like:

<span custom-button></span>

So above span will be replaced by html value used in template attribute of directive. And you will see a button labeled "Click Me".

When  you clicked on that button you should see a alert.


AngularJS get attribute value in directive

<a check-permission permissioncode="BILLING_MANAGE">Billing</a>


In above line directive name is check-permission and we want value of attribute permissioncode in directive.

Using below code we can get the value of attribute permissioncode:

app.directive('
checkPermission', function() {
    return function(scope, element, attrs) {
        attrs.$observe('permissioncode', function(permission) {
              alert(permission)
        });
    }
});