1

I am trying to change my textbox value after click on a button using javascript and change ng-model value of angular js.

The Javascript code working fine and change the textbox value, but I also want to change my ng-model value according to the text box.

<!doctype html>
<html ng-app>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.min.js"></script>
    <script>
        function tryi()
        {
             document.getElementById('newtry').value="any value";
        }
    </script>
  </head>
  <body>
    <div>
      <label>Name:</label>
      <input type="text" ng-model="yourName" id="newtry">
      <input type="button" onclick="tryi();"value="click here">
      <hr>
      <h1>Hello {{yourName}}!</h1><hr>
    </div>
  </body>
</html>
4
  • What is the sense behind doing this outside of the angular scope? Commented Mar 22, 2014 at 7:28
  • in other project i set one textbox value using javascript so that can not assign in ng-model so i have that type of problem. Commented Mar 22, 2014 at 7:32
  • You can also set the Textbox with JavaScript from within an Angular-Controller, then you wouldnt have this problem. If you want to adjust a mode-value from OUTSIDE the angular-scope, you would need to use the $apply()-Method to notify angular about the changes. But i would recommend you read the Angular-Tutorial first (docs.angularjs.org/tutorial), that explains how it works. Commented Mar 22, 2014 at 8:05
  • Read the docs before you ask these kind of questions. Commented Mar 22, 2014 at 9:32

1 Answer 1

2

I think you have included angular-resource and not included angularjs

add this line

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>

the code is working fine

Here is the plunkr link: http://plnkr.co/edit/CPX1Mhn2CkmY6wZWHvya?p=preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.