AngularJS Populating input field with directive but not captured in $scope

Ryan W Kan

When I populate an input field from within a directive, it shows on the DOM, but $scope is not capturing the value. How do I fix it so that $scope captures the new info?

How to reproduce the behaviour from my Fiddle:

Click Fill with Directive
Click Log
--> $scope.input val is undefined

Fill input with 'asdf'
Click Log
--> $scope.input val is 'asdf'

Fill input with 'abcd'
Click Fill with Directive
--> DOM shows 'quick brown fox'
Click Log
--> $scope.input val is 'abcd'

My code is here: JSFiddle

JS:

'use strict';

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

app.controller('myCtrl', function($scope){
    $scope.logResult = function(){
        console.log($scope.input);
    }
});

app.directive('fillInput', function(){
    return {
        link: function($scope, elem){
            $scope.fillFormDirective = function() {
                console.log(elem);
                elem.val('quick brown fox');
            }        
        }
    };
});

HTML:

<div ng-controller='myCtrl'>

    <div>
        <input type="text" ng-model="input.weird" id="inputField" fill-input />
    </div>

    <div>
        <button ng-click="fillFormDirective()" >Fill With Directive</button>
    </div>   

    <div>
        <button ng-click="logResult()">Log Result</button>
    </div>

</div> 
Ryan W Kan

Solution from this SO thread:

Update Angular model after setting input value with jQuery

Working Plunkr: http://plnkr.co/edit/QKDaHT3CllyBtrRPdYUr?p=preview

Basically, quoting @Stewie,

ngModel listens for "input" event, so to "fix" your code you'd need to trigger that event after setting the value:

      elem.bind("click", function(e){
        console.log(elem);

        newElem.val('quick brown fox');
        newElem.triggerHandler('input'); <-----This line of code fixed the problem
      });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using AngularJS directive to format input field while leaving scope variable unchanged

From Dev

angularjs input field directive isn't clearing errors when scope changes value

From Dev

Angularjs: Form validation on input field generated by directive

From Dev

AngularJs validate form with input field from directive

From Dev

Angularjs: Form validation on input field generated by directive

From Dev

AngularJS - Getting an input value and passing it to parent directive scope

From Dev

Change placeholder of input from angularjs directive wake scope model 'undefined'

From Dev

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

From Dev

angularjs directive isolated scope

From Dev

AngularJS : directive scope inheritance

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 Scope and tab directive

From Dev

AngularJS - Directive/Scope Issue

From Dev

AngularJS directive scope not updating

From Dev

AngularJS : how to get directive input field model value in controller?

From Dev

Populating input field with JSON data

From Dev

Populating directive with the results of an $http call using AngularJS

From Dev

AngularJS - how to sync result of calculated input field to a scope variable

From Dev

AngularJS: extend input directive

From Dev

AngularJs directive not updating another directive's scope

From Dev

AngularJS initializing $scope of directive/controller

From Java

AngularJS : Differences among = & @ in directive scope?

From Dev

AngularJS Directive scope gets overwriten

From Dev

AngularJS Directive with Scope data array

From Dev

Binding directive scope with controller in AngularJS

From Dev

What is the default for AngularJS directive scope?

From Dev

Angularjs TreeView Directive With Isolated Scope

Related Related

  1. 1

    Using AngularJS directive to format input field while leaving scope variable unchanged

  2. 2

    angularjs input field directive isn't clearing errors when scope changes value

  3. 3

    Angularjs: Form validation on input field generated by directive

  4. 4

    AngularJs validate form with input field from directive

  5. 5

    Angularjs: Form validation on input field generated by directive

  6. 6

    AngularJS - Getting an input value and passing it to parent directive scope

  7. 7

    Change placeholder of input from angularjs directive wake scope model 'undefined'

  8. 8

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

  9. 9

    angularjs directive isolated scope

  10. 10

    AngularJS : directive scope inheritance

  11. 11

    AngularJS Isolated Scope Directive

  12. 12

    Angularjs Scope Issue with Directive

  13. 13

    AngularJS: Indicate the scope of a directive

  14. 14

    AngularJS Scope and tab directive

  15. 15

    AngularJS - Directive/Scope Issue

  16. 16

    AngularJS directive scope not updating

  17. 17

    AngularJS : how to get directive input field model value in controller?

  18. 18

    Populating input field with JSON data

  19. 19

    Populating directive with the results of an $http call using AngularJS

  20. 20

    AngularJS - how to sync result of calculated input field to a scope variable

  21. 21

    AngularJS: extend input directive

  22. 22

    AngularJs directive not updating another directive's scope

  23. 23

    AngularJS initializing $scope of directive/controller

  24. 24

    AngularJS : Differences among = & @ in directive scope?

  25. 25

    AngularJS Directive scope gets overwriten

  26. 26

    AngularJS Directive with Scope data array

  27. 27

    Binding directive scope with controller in AngularJS

  28. 28

    What is the default for AngularJS directive scope?

  29. 29

    Angularjs TreeView Directive With Isolated Scope

HotTag

Archive