AngularJs directive, scope with value and function

gervais.b

I have a directive myEditable that toggle a <div> with an <input type=text> to allow inline edition :

<my-editable value="vm.contact.name"></my-editable>

I was happy with it until I read some articles that say that $scope.$apply should not be used. I'm using it when the user save his changes to update the model (vm.contact.name in my case) :

function save() {
  scope.$apply(function(){
    scope.value = editor.find('input').val();
  });
  toggle();
}

But since it is a bad thing, I would like to pass a callback method to my directive. This callback must be called with the new value when the user save his changes. However, it seems that I cannot add two fields to the directive scope :

return {
  restrict: 'EA',
  scope: {
    value: '=value'/*,
    onSave: '&onSave'*/
  },
  link: function(scope, element, attr) {
    // ...
    element.find('.save').click(function(){
      save();
    });

    // Declaration of `save` as above.
  }
}

If I uncomment onSave then the value is never received and onSave is undefined.

My question is, how can I give a value and a callback method to a directive ? And, as bonus, how can I pass parameters to the callback ?

Thanks

Rishi Tiwari

You can pass 'n' number of fields in directives isolated scope.

If you want to pass a function use &. Keep this in mind if your property name is onSave then in the view use it like this on-save.

Your directive should look like below

app.directive('dir',function(){
  return {
  restrict: 'EA',
  scope: {

    onSave: '&'
  },
  link: function(scope, element, attr) {
    // ...
    debugger
    console.log(scope.onSave)
    scope.onSave();

    // Declaration of `save` as above.
  }
}

})

In the view you can pass the function like below

 <dir on-Save='abc()'/>

OR

 <dir on-save='abc()'/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing value of a variable to angularjs directive template function

From Dev

angularjs directive isolated scope

From Dev

AngularJS directive scope not updating when value changed outside of AngularJS

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

AngularJS : directive scope inheritance

From Dev

AngularJS : How to change scope value from isolated scope within directive

From Dev

AngularJS directive Passing parameters to isolated scope function

From Dev

Not able to update the `scope` value from directive function

From Dev

AngularJs: Directive with function for template property. How to get scope value?

From Dev

How to set a default value in an AngularJS Directive Scope?

From Dev

AngularJS directive scope function not being called

From Dev

Change controller scope value from directive AngularJS

From Dev

AngularJs directive, scope with value and function

From Dev

AngularJS Isolated Scope Directive

From Dev

Angularjs Scope Issue with Directive

From Dev

AngularJS: Indicate the scope of a directive

From Dev

angularjs directive scope function not working

From Dev

AngularJS Scope and tab directive

From Dev

AngularJS - Directive/Scope Issue

From Dev

AngularJS directive scope not updating

From Dev

angularjs scope function of a repeated directive

From Dev

AngularJS directive Passing parameters to isolated scope function

From Dev

AngularJs: Directive with function for template property. How to get scope value?

From Dev

AngularJS directive scope function not being called

From Dev

angularjs directive + apply scope value in many controller

From Dev

AngularJs directive compile function and isolated scope

From Dev

angularjs controller access directive scope value

From Dev

Set the value in a link function of a directive to AngularJs model

From Dev

how to bind directive scope value to template in angularjs

Related Related

  1. 1

    Passing value of a variable to angularjs directive template function

  2. 2

    angularjs directive isolated scope

  3. 3

    AngularJS directive scope not updating when value changed outside of AngularJS

  4. 4

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  5. 5

    AngularJS : directive scope inheritance

  6. 6

    AngularJS : How to change scope value from isolated scope within directive

  7. 7

    AngularJS directive Passing parameters to isolated scope function

  8. 8

    Not able to update the `scope` value from directive function

  9. 9

    AngularJs: Directive with function for template property. How to get scope value?

  10. 10

    How to set a default value in an AngularJS Directive Scope?

  11. 11

    AngularJS directive scope function not being called

  12. 12

    Change controller scope value from directive AngularJS

  13. 13

    AngularJs directive, scope with value and function

  14. 14

    AngularJS Isolated Scope Directive

  15. 15

    Angularjs Scope Issue with Directive

  16. 16

    AngularJS: Indicate the scope of a directive

  17. 17

    angularjs directive scope function not working

  18. 18

    AngularJS Scope and tab directive

  19. 19

    AngularJS - Directive/Scope Issue

  20. 20

    AngularJS directive scope not updating

  21. 21

    angularjs scope function of a repeated directive

  22. 22

    AngularJS directive Passing parameters to isolated scope function

  23. 23

    AngularJs: Directive with function for template property. How to get scope value?

  24. 24

    AngularJS directive scope function not being called

  25. 25

    angularjs directive + apply scope value in many controller

  26. 26

    AngularJs directive compile function and isolated scope

  27. 27

    angularjs controller access directive scope value

  28. 28

    Set the value in a link function of a directive to AngularJs model

  29. 29

    how to bind directive scope value to template in angularjs

HotTag

Archive