Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dapperdandev/b85b214f59043d1a040f904adadff84c to your computer and use it in GitHub Desktop.
Save dapperdandev/b85b214f59043d1a040f904adadff84c to your computer and use it in GitHub Desktop.
AngularJS Directive Bindings Examples

NGJS Directive Bindings Examples

One Way Down

<one-way-down name="Daniel"></one-way-down>
...
  .directive('oneWayDown', function () {
    return {
      restrict: 'E',
      scope: {
        name: '@' // 
      },
      template: /*html*/`
    <div>Hello {{name}}</div>
  `,
      link: (scope, element, attrs) => {
      }
    }
  })
...

Two Way

<two-way name="{{name}}"></two-way>
...
  .controller('appController', ['$scope', function ($scope) {
    $scope.name = 'test';
  }])

  .directive('twoWay', function () {
    return {
      restrict: 'E',
      scope: {
        name: '@' // 
      },
      template: /*html*/`
    <div>Hello {{name}}</div>
  `,
      link: (scope, element, attrs) => {
      }
    }
  })
...

One Way Up

<one-way-up get-name="doSomething()"></one-way-up>
...
  .controller('appController', ['$scope', function ($scope) {
    $scope.doSomething = () => {
      console.log('doing something');
    }
  }])

  .directive('oneWayUp', function () {
    return {
      restrict: 'E',
      scope: {
        getName: '&'
      },
      template: /*html*/`
    <button ng-click="getName()">Get Name</button>`,
      link: (scope, element, attrs) => {
      }
    }
  })
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment