1

I 've just started to study Angular. Consider the following simple template:

<div ng-app ng-init="qty=1;cost=2">
  <div>
    Quantity: <input id="quantity" type="number" min="0" data-ng-model="qty">
  </div>
  <div>
    Costs: <input type="number" min="0" data-ng-model="cost">
  </div>
  <div>
    Total: {{qty * cost | currency}}
  </div>
</div>

I 've tried the following in Chrome's console:

var $scope = angular.element($('#quantity')).scope();
$scope.qty; //1

By changing the first input text from 1 to 3 manually, I am getting:

$scope.qty; //3

So far, so good! First question: Why the opposite does not work? Namely,by setting $scope.qty=5; input text does not change.

Second question: By applying e.g. $('#quantity').val(12); the input text changes, however the expression in Total is not recalculated and DOM is not refreshed!

4 Answers 4

2

Rules of AngularJS Club:

  1. You should not use jQuery
  2. You should not use jQuery
  3. If you do you do some hacks with scope in console, don't forget to run the digest circle: .

    $scope.$apply()

.

.

.

P.S.: you don't need jQuery here as well:

angular.element('#quantity').scope();
Sign up to request clarification or add additional context in comments.

Comments

0

Answer to both question: Angular bind after a $digest phase.

Comments

0

Please see the following code. Each time the value change, it is reflected in the UI and vice versa.

HTML

<div ng-app='App'>
  <div ng-controller='AppCtrl'>
    <form ng-submit='ChangePrice()'>
      Quantity: <input id="quantity" type="number" min="0" data-ng-model="qty">
      Costs: <input type="number" min="0" data-ng-model="cost">
      Total: {{qty * cost | currency}}
      <input type='submit' value='Change Price'></input>
    </form>
  </div>
</div>

JS

 var helloApp = angular.module('App', []);
 helloApp.controller('AppCtrl', function($scope) {
    $scope.qty=10;
    $scope.cost = 2;

   $scope.ChangePrice = function(){
      $scope.cost =  $scope.cost + 1;
   }
 });

Comments

0

I like the other answers, but I think they are not answering your questions.

First question: Why the opposite does not work? If you just change a variable by hand over the console no framework will know about that changes. If you change the scope you should also use $scope.$apply() afterwards.

Second question: By applying e.g. $('#quantity').val(12); the input text changes, however the expression in Total is not recalculated and DOM is not refreshed! Are you sure that the DOM is not refreshed, when you apply a jQuery command on your console? This command also have the refresh inside. With that you are manipulating directly the page.

So your first command does not have a refresh inside and your second has.

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.