5

This is a very newbie question but why doesn't

    document.getElementById("demo").value 

update angular model? It updates the input field, but it updates the model only after manually putting something into the field.

Here's the code:

<div ng-controller="AddCtrl">
  <input type="text" id="demo" ng-model="test.fld" ng-change="change()"></input>
  <br />

  [ <a href="" onclick="somefnc()">pop</a> ] 
  [ <a href="" ng-click="add(test)">add</a> ]       
  <p>Test-field: {{test.fld}}</p>
</div>



<script>
  function somefnc() {
    document.getElementById("demo").value = "hi";  
  }
</script>
1
  • Did you get this to work? I tried the idea below but it still does not update. Commented Jul 19, 2015 at 21:23

2 Answers 2

4

The answer is purely related to the way angular binds data. The simplest answer would be, after manually changing the form data, $scope.$apply() must be called for the element's scope. Called outside the "Angular world" you could change somefnc to:

function somefnc() {
    document.getElementById("demo").value = "hi";
    $('#demo').scope().$apply();  
}

Best practices say that any direct dom manipulation should happen only in directives. Ideally, you would change this value in the controller with something like:

$scope.somefnc = function(){
    test.fld = "hi";
}

Anything done through the angular scope like this does not need $scope.$apply, only direct dom manipulation does.

The full example is here

<div ng-controller="AddCtrl">
  <input type="text" id="demo" ng-model="test.fld" ng-change="change()"></input>
  <br />

  [ <a href="" ng-click="somefnc()">pop</a> ] 
  [ <a href="" ng-click="add(test)">add</a> ]       
  <p>Test-field: {{test.fld}}</p>
</div>



<script>
angular.module('app').controller('AddCtrl',['$scope',function($scope){
    $scope/test = {fld: ''};
    $scope.add = function(){
        $scope.test.fld += test.fld
    }
    $scope.change = function(){
        alert('I changed!');
    }
    $scope.somefnc = function() {
        $scope.test.fld = "hi";  
    }
}])
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to trigger validation after changing the value of the input on a site that has validation. The scope apply does not trigger the validation. Is there something else I can use to trigger the validation in the case of the html here: <input id="shippingAddr-first-name" type="text" class="first-name ng-pristine ng-valid" maxlength="16" data-ng-model="addressTypes[addressType].FirstName" focus-me="commonAddressTypeProps[addressType].focusOnFirstName" client-validation="onExit" data-required-on-exit="">
0

Because AngularJS can only watch the model change in AngularJS's scope. So you need to modify using Angular's way not other ways like jQuery if you want the 2-way data binding magically work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.