How to pass value back from angularjs directive scope with javascript event

Aldarund

What Im trying to accomplish: I have jQuery plugin that I want to wrap to be an angular directive. And to do this i need to pass params to it, and the plugin have it ownonchange` even where I'm trying to change passed values so it will be reflected in the original scope. And I get some really unexpected and strange results. Here is fiddle number one:

http://jsfiddle.net/q1915b38/2/

Here I tried to simulate minimal example of what i want to accomplish. But as you see it just does not work at all. Value in the original controller scope doesn't change. But in real world example it act a bit differently. And here goes fiddle number 2.

http://jsfiddle.net/ne5hbgxp/

The only thing i changed from first one is template from template:

template: "<input type='text' id='blah' />",

to

template: "<input type='text' id='blah' ng-model='abc' />",

Basically i added to template an ng-model attribute which I don't use anywhere at all. But it just goes from totally not working to working with glitches. Now when change trigger first time nothing happens. But when it triggers second time - value from previous change got passed into original scope. When I change 3 time value - the value from second time goes to controller. And so on. So basically it have a delay with one step back for unknown for me reason. And this is exact behavior that I face in my real world example, although there no ng-model at all and all content generate via jQuery plugin.

So basically my questions are following:

1) Why its not working in first example

2) Why its working in second example with this strange behavior with one step delay? What the logic on this behavior?

3) What is a correct way to solve this ?

scniro

Since you're using jQuery to update something in your directive, a call to $apply() is needed to trigger an angular digest cycle

link: function(scope, iElement, iAttrs, controller) {
    $('#blah').change(function() {
        scope.value = $(this).val();
        scope.$apply();
    });
}

JSFiddle Link

However looking at this a bit closer, is there a reason why you prefer jQuery .change() in this example? Angular offers ngChange, which may be just what you are looking for, since you will be alleviated from explicitly calling a digest cycle since we're in Angular world and not battling jQuery so to speak. An example may include...

<input type='text' id='blah' ng-model='abc' ng-change='update()'/>

scope.update = function() {
    scope.value = scope.abc;
}

JSFiddle Link with ng-change

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass value back from angularjs directive scope with javascript event

From Dev

AngularJs #how to pass scope variable in on change event in directive in input file

From Dev

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

From Dev

How can I get pass value from controller back to directive's template attribute in AngularJS?

From Dev

How can I get pass value from controller back to directive's template attribute in AngularJS?

From Dev

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

From Dev

How to pass object from controller to directive without using $scope in angularJS

From Dev

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

From Dev

Change controller scope value from directive AngularJS

From Dev

How to pass scope value to the directive controller

From Dev

How to pass a value from an AngularJS directive to a function in parent controller

From Dev

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

From Dev

how to bind directive scope value to template in angularjs

From Dev

How to pass a variable from $scope into a directive, and reverse?

From Dev

How to pass scope from directive to template

From Dev

Angularjs: Pass calendar value from directive to controller

From Dev

How to pass $event from custom directive to function?

From Dev

Pass object context back to controller callback from AngularJS Directive

From Dev

AngularJS - How to access $scope from a directive with specified scope

From Dev

AngularJS - How to pass different $scope attributes to a directive through attrs

From Dev

Angularjs how to pass data from a controller to a directive

From Dev

How to pass $scope value to custom filter in AngularJs?

From Dev

AngularJs directive, scope with value and function

From Dev

AngularJs directive, scope with value and function

From Dev

Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

From Dev

How to change all directive scope value from any one of directive?

From Dev

AngularJS : Change parent scope value from custom directive

From Dev

AngularJS : directive binding value to parent scope from template model

From Dev

AngularJS : directive binding value to parent scope from template model

Related Related

  1. 1

    How to pass value back from angularjs directive scope with javascript event

  2. 2

    AngularJs #how to pass scope variable in on change event in directive in input file

  3. 3

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

  4. 4

    How can I get pass value from controller back to directive's template attribute in AngularJS?

  5. 5

    How can I get pass value from controller back to directive's template attribute in AngularJS?

  6. 6

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

  7. 7

    How to pass object from controller to directive without using $scope in angularJS

  8. 8

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

  9. 9

    Change controller scope value from directive AngularJS

  10. 10

    How to pass scope value to the directive controller

  11. 11

    How to pass a value from an AngularJS directive to a function in parent controller

  12. 12

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

  13. 13

    how to bind directive scope value to template in angularjs

  14. 14

    How to pass a variable from $scope into a directive, and reverse?

  15. 15

    How to pass scope from directive to template

  16. 16

    Angularjs: Pass calendar value from directive to controller

  17. 17

    How to pass $event from custom directive to function?

  18. 18

    Pass object context back to controller callback from AngularJS Directive

  19. 19

    AngularJS - How to access $scope from a directive with specified scope

  20. 20

    AngularJS - How to pass different $scope attributes to a directive through attrs

  21. 21

    Angularjs how to pass data from a controller to a directive

  22. 22

    How to pass $scope value to custom filter in AngularJs?

  23. 23

    AngularJs directive, scope with value and function

  24. 24

    AngularJs directive, scope with value and function

  25. 25

    Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

  26. 26

    How to change all directive scope value from any one of directive?

  27. 27

    AngularJS : Change parent scope value from custom directive

  28. 28

    AngularJS : directive binding value to parent scope from template model

  29. 29

    AngularJS : directive binding value to parent scope from template model

HotTag

Archive