How to bind an attribute in a AngularJS directive?

James Curran

I've defined a Angular directive, which seems to be working fine --- provided I hard-code it's attribute value. For example:

<notes-history application-id="11"></notes-history>
{{applicationid}}
<notes-history application-id="{{applicationid}}"></notes-history>

The first <notes-history> directive works fine. The plain text binding works fine. However, when the directive's link() function is called the second time, the application-id (based on the 3rd parameter passed to link()) is an empty string. How do I get it to bind to the attribute?

Update:

Here's the (mostly) full code for the directive. (Javascript generated from TypeScript)

var notesHistory = (function () {
    function notesHistory(notesService, alertsService) {
        this.notesService = notesService;
        this.alertsService = alertsService;
        var directive = {};
        directive.priority = 0;
        directive.restrict = "EA";
        directive.scope = { applicationId: "@applicationId" };
        directive.replace = true;
        directive.transclude = false;
        directive.templateUrl = "app/views/notesHistory.html";
        directive.link = function (scope, instanceElement, instanceAttributes, controller, transclude) {
            // elided.     
            // instanceAttributes.applicationId is correct here 
            // when hard-code, and '' when bound.
            });
        };
        return directive;
    }
    return notesHistory;
})();
app.directive("notesHistory", ["notesService", "alertsService", notesHistory]);

Update 2

I have actually figured out my problem, and it turns out to be unrelated to the directive.

The problem was the controller's first (and essentially, only) action was to make an async web service call to get the data. So the order of events was:

  1. Run Controller (launch webservice call)
  2. Run directive (using data)
  3. Process response from webservice (receiving data needed for directive)

Since this has become a completely different problem, I'll create a new question for it.

Ninjarabbi

It should work. Here's a super simple plunkr that examplifies it: http://plnkr.co/edit/tYFbxxAxwLXAzaNyCOwE?p=preview

HTML

<body ng-controller="MainCtrl">
  <p>Hello {{name}}!</p>
  <test foo="bar"></test>
  <test foo={{name}}></test>
</body>

JS

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

app.directive('test', function() {
  return {
    scope: {
      foo: '@'
    },
    template: 'Name: {{foo}}'
  };
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS: How to monitor an attribute in a directive?

From Dev

How to create a Attribute Directive to insert another attribute directive in a element in AngularJS

From Dev

Angularjs directive add directives as attribute and bind them dynamically

From Dev

how to bind directive scope value to template in angularjs

From Dev

How to conditionally apply attribute in angularJS directive?

From Dev

How do you 'require' an attribute on an angularjs directive?

From Dev

AngularJS: How to set an attribute to a custom directive

From Dev

Angularjs directive: how to pass an array via attribute?

From Dev

How to conditionally apply attribute in angularJS directive?

From Dev

How to bind array with ng-bind-html directive in angularJS?

From Dev

How to bind a multiple word attribute in an AngularJS component

From Dev

AngularJs: How to get the attribute value of a attribute type directive

From Dev

AngularJs: How to get the attribute value of a attribute type directive

From Dev

Attribute directive in AngularJS

From Dev

How do I bind to a custom event in an angularjs directive?

From Dev

How do I bind to a custom event in an angularjs directive?

From Dev

Angularjs - how do i access directive attribute in controller

From Dev

How to get attribute value in template in angularjs custom directive?

From Dev

AngularJS: how do I code an element directive with optional attribute parameters?

From Dev

AngularJS: How to access input[date] min attribute within custom directive?

From Dev

AngularJS directive, Call service by directive attribute

From Dev

AngularJS directive as attribute - change array in directive

From Dev

AngularJS : Directive not picking up attribute

From Dev

Attribute without value in AngularJS directive

From Dev

AngularJS: Add attribute, compile directive

From Dev

AngularJS - Use attribute directive conditionally

From Dev

Check existence of attribute in AngularJs Directive

From Dev

AngularJS : directive watch attribute expression

From Dev

angularjs nested directive with changing attribute

Related Related

  1. 1

    AngularJS: How to monitor an attribute in a directive?

  2. 2

    How to create a Attribute Directive to insert another attribute directive in a element in AngularJS

  3. 3

    Angularjs directive add directives as attribute and bind them dynamically

  4. 4

    how to bind directive scope value to template in angularjs

  5. 5

    How to conditionally apply attribute in angularJS directive?

  6. 6

    How do you 'require' an attribute on an angularjs directive?

  7. 7

    AngularJS: How to set an attribute to a custom directive

  8. 8

    Angularjs directive: how to pass an array via attribute?

  9. 9

    How to conditionally apply attribute in angularJS directive?

  10. 10

    How to bind array with ng-bind-html directive in angularJS?

  11. 11

    How to bind a multiple word attribute in an AngularJS component

  12. 12

    AngularJs: How to get the attribute value of a attribute type directive

  13. 13

    AngularJs: How to get the attribute value of a attribute type directive

  14. 14

    Attribute directive in AngularJS

  15. 15

    How do I bind to a custom event in an angularjs directive?

  16. 16

    How do I bind to a custom event in an angularjs directive?

  17. 17

    Angularjs - how do i access directive attribute in controller

  18. 18

    How to get attribute value in template in angularjs custom directive?

  19. 19

    AngularJS: how do I code an element directive with optional attribute parameters?

  20. 20

    AngularJS: How to access input[date] min attribute within custom directive?

  21. 21

    AngularJS directive, Call service by directive attribute

  22. 22

    AngularJS directive as attribute - change array in directive

  23. 23

    AngularJS : Directive not picking up attribute

  24. 24

    Attribute without value in AngularJS directive

  25. 25

    AngularJS: Add attribute, compile directive

  26. 26

    AngularJS - Use attribute directive conditionally

  27. 27

    Check existence of attribute in AngularJs Directive

  28. 28

    AngularJS : directive watch attribute expression

  29. 29

    angularjs nested directive with changing attribute

HotTag

Archive