Click here to Skip to main content
15,914,387 members
Home / Discussions / JavaScript
   

JavaScript

 
QuestionTwo way binding in AngularJS Pin
Member 1110597416-Dec-16 4:50
Member 1110597416-Dec-16 4:50 
I'm trying to understand two way binding in AngularJS but I'm unable to understand the behavior of functions and variables.

Consider the following example:

So, I have a controller like this:

JavaScript
var app=angular.module("cal", []);
        app.controller("calc",function ($scope) {
        $scope.result=Number($scope.a || 0)+ Number($scope.b || 0);
      });


and HTML like this:
<div ng-app="cal">
<div  ng-controller="calc">


<input type="text" ng-model="a"/>
<input type="text" ng-model="b"/>

<span>{{result}}</span>
</div>
</div>


This code doesn't update the variable "result".

However, if I use something like this:

JavaScript
var app=angular.module("cal", []);
        app.controller("calc",function ($scope) {
       $scope.add = function(){
     return (Number($scope.a || 0)+ Number($scope.b || 0));
}
      });


and


<div ng-app="cal">
<div  ng-controller="calc">


<input type="text" ng-model="a"/>
<input type="text" ng-model="b"/>

<span>{{add()}}</span>
</div>
</div>

the value in gets updated automatically with variation in variables "a" or "b".

How does this work? What's the difference in returning from the function and using the variable directly in the DOM?

AnswerRe: Two way binding in AngularJS Pin
Nathan Minier21-Dec-16 3:54
professionalNathan Minier21-Dec-16 3:54 
GeneralRe: Two way binding in AngularJS Pin
Member 1110597421-Dec-16 4:30
Member 1110597421-Dec-16 4:30 
GeneralRe: Two way binding in AngularJS Pin
Nathan Minier21-Dec-16 4:44
professionalNathan Minier21-Dec-16 4:44 
GeneralRe: Two way binding in AngularJS Pin
Member 1110597421-Dec-16 4:57
Member 1110597421-Dec-16 4:57 
GeneralRe: Two way binding in AngularJS Pin
Nathan Minier21-Dec-16 5:12
professionalNathan Minier21-Dec-16 5:12 
GeneralRe: Two way binding in AngularJS Pin
Member 1110597422-Dec-16 4:40
Member 1110597422-Dec-16 4:40 
GeneralRe: Two way binding in AngularJS Pin
Nathan Minier23-Dec-16 1:01
professionalNathan Minier23-Dec-16 1:01 
Questionnpm install is giving error that project.json file doesn't exist even it exists Pin
indian1435-Dec-16 7:35
indian1435-Dec-16 7:35 
AnswerRe: npm install is giving error that project.json file doesn't exist even it exists Pin
John C Rayan30-Dec-16 4:38
professionalJohn C Rayan30-Dec-16 4:38 
QuestionSend as .pdf email attachment, job application form with asp.net Pin
TaRoshka3-Dec-16 0:43
TaRoshka3-Dec-16 0:43 
AnswerRe: Send as .pdf email attachment, job application form with asp.net Pin
Nathan Minier3-Dec-16 3:49
professionalNathan Minier3-Dec-16 3:49 
Questionweb developer Pin
Member 1284786913-Nov-16 6:37
Member 1284786913-Nov-16 6:37 
AnswerRe: web developer Pin
Richard MacCutchan13-Nov-16 21:43
mveRichard MacCutchan13-Nov-16 21:43 
AnswerRe: web developer Pin
ZurdoDev16-Nov-16 1:15
professionalZurdoDev16-Nov-16 1:15 
QuestionVimeo Sample code to fetch private videos using REST/ JSON/ JQuery Pin
Elena200612-Nov-16 0:03
Elena200612-Nov-16 0:03 
AnswerRe: Vimeo Sample code to fetch private videos using REST/ JSON/ JQuery Pin
ZurdoDev15-Nov-16 2:00
professionalZurdoDev15-Nov-16 2:00 
QuestionHow do I get apis for Facebook, Google talk, skype, whatsapp for my project using javascript and html Pin
Member 1233604411-Nov-16 17:36
Member 1233604411-Nov-16 17:36 
AnswerRe: How do I get apis for Facebook, Google talk, skype, whatsapp for my project using javascript and html Pin
Richard MacCutchan11-Nov-16 21:55
mveRichard MacCutchan11-Nov-16 21:55 
GeneralRe: How do I get apis for Facebook, Google talk, skype, whatsapp for my project using javascript and html Pin
Member 1233604412-Nov-16 0:13
Member 1233604412-Nov-16 0:13 
QuestionRe: How do I get apis for Facebook, Google talk, skype, whatsapp for my project using javascript and html Pin
Richard MacCutchan12-Nov-16 0:22
mveRichard MacCutchan12-Nov-16 0:22 
AnswerRe: How do I get apis for Facebook, Google talk, skype, whatsapp for my project using javascript and html Pin
Member 1233604412-Nov-16 1:53
Member 1233604412-Nov-16 1:53 
GeneralRe: How do I get apis for Facebook, Google talk, skype, whatsapp for my project using javascript and html Pin
Richard MacCutchan12-Nov-16 2:05
mveRichard MacCutchan12-Nov-16 2:05 
AnswerRe: How do I get apis for Facebook, Google talk, skype, whatsapp for my project using javascript and html Pin
biji cabe16-Dec-16 18:23
biji cabe16-Dec-16 18:23 
GeneralRe: How do I get apis for Facebook, Google talk, skype, whatsapp for my project using javascript and html Pin
nanik8817-Dec-16 21:30
nanik8817-Dec-16 21:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.