Angular JS observe on directive attribute

前端 未结 3 1056
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 01:36

How can angular js watch attributes on custom directive in order to accept angular values to be bind

Here is what I have so far:



        
相关标签:
3条回答
  • 2020-12-30 02:00

    You can also use a new attribute for your directive instead of data-value:

    <tile title="Sleep Duration"  your-new-attribute={{sleepHistory.averageSleepTime}}" />
    
    attrs.$observe('yourNewAttribute', function (newValue, oldValue) {
        if (newValue && newValue !== oldValue) {
            // ...
        }
    });
    
    0 讨论(0)
  • 2020-12-30 02:05

    From https://docs.angularjs.org/api/ng/type/$compile.directive.Attributes:

    all of these are treated as equivalent in Angular:

    <span ng:bind="a" ng-bind="a" data-ng-bind="a" x-ng-bind="a">
    

    So the attribute data-value normalizes to value

    So, this is what you want:

    attrs.$observe('value', function(val) {
    
    0 讨论(0)
  • 2020-12-30 02:10

    Just watch the value instead of dataValue.

    attrs.$observe('value', function (val)  { ...
    
    0 讨论(0)
提交回复
热议问题