1

Im trying to output the following info and have it update realtime using angulars automatic binding. the first and last update ok, but full isnt updating as I would expect. im grateful for any assistance.

http://jsfiddle.net/laurencefass/74u313gx/1/ required output:

first = john
last = doe
full name=john doe.

HTML

<div ng-app="nameApp">
  <div ng-controller="nameController">
    <input ng-model="first"/>
    <p>first = {{first}}</p>
    <input ng-model="last"/>
    <p>last = {{last}}</p>
    <p>full name = {{full}}</p>
 </div>

JS

var app=angular.module("nameApp", []);

app.controller('nameController', function($scope) {
  $scope.first="john";
  $scope.last="doe";
  $scope.full = $scope.first + " " + $scope.last;
});

initial output seems correct and the first and last update as expected. but the full name is not updating despite being a $scope var and a product of first and last.

4
  • you have misunderstood with this full name is binded once you will have keep watch to update full name. Commented Oct 29, 2015 at 12:13
  • sorry i dont understand can you offer an example? Commented Oct 29, 2015 at 12:14
  • where did i specify one-time only binding on full name? Commented Oct 29, 2015 at 12:15
  • 1
    controller only runs once per instance. full won't ever change in your code as is Commented Oct 29, 2015 at 12:26

4 Answers 4

3

The following line is only run once. So it is initiated with the first value that has been assigned to first and last.

$scope.full = $scope.first + " " + $scope.last;

So, if you want your binding to work, without having a unnecessary fonction into your controller. (keep your controllers as clean as possible!)

<div ng-app="nameApp">
  <div ng-controller="nameController">
    <input ng-model="first"/>
     <p>first = {{first}}</p>
     <input ng-model="last"/>
     <p>last = {{last}}</p>
    <p>full name = {{first + ' ' + last}}</p>
  </div>
</div>

Have a look at your fiddle :

http://jsfiddle.net/74u313gx/2/

If you really need to have the fullname into the controller, you can use a $watch therefor :

$scope.$watch('first', updateFull);
$scope.$watch('last', updateFull);
function updateFull(){
    $scope.full = $scope.first + " " + $scope.last;
}

If you are concerned by performance, you may want to avoid defining to much watches, then a ng-change can be used :

controller :

$scope.updateFull = function(){
    $scope.full = $scope.first + " " + $scope.last;
}

View :

<div ng-app="nameApp">
  <div ng-controller="nameController">
    <input ng-model="first" ng-change="updateFull();"/>
    <p>first = {{first}}</p>
    <input ng-model="last" ng-change="updateFull();"/>
    <p>last = {{last}}</p>
    <p>full name = {{full}}</p>
 </div>
Sign up to request clarification or add additional context in comments.

1 Comment

Deblaton beat me to the punch on advising not you use $watch so I'd just like to suggest this article as some further reading to explain in detail why not benlesh.com/2013/10/title.html
3

Since you're defining a string on $scope that is concatenated with several parameters, it won't change automatically when you change the parts that you used to assemble it with.

If you want to achieve what you're looking for, you can do one of two things:

One:

<p>full name = {{first + ' ' + last}}</p>

Two:

<p>full name = {{getFullName()}}</p>

And in the controller have a function:

$scope.getFullName = function () {
    return $scope.first + ' ' + $scope.last;
}

2 Comments

ok the One seems really obvious i wish i had seen that but i guess Two helps me understand how angular is working. many thanks.
@LaurenceFass Pleasure :)
1

it will not update auto since your are passing a string in full name after concatenation it returns simple string not an angular var, you need to do the following to update value automatically.

 $scope.$watchGroup(["first","last"],function(){
     $scope.full = $scope.first + " " + $scope.last;
  });

Comments

1

update your code like below:

View Update

<div ng-app="nameApp">
<div ng-controller="nameController">
<input ng-model="first" ng-change="change()"/>
 <p>first = {{first}}</p>
 <input ng-model="last" ng-change="change()" />
 <p>last = {{last}}</p>
<p>full name = {{full}}</p>

Script Update

var app=angular.module("nameApp", []);


app.controller('nameController', function($scope) {
    $scope.first="john";
    $scope.last="doe";
    $scope.full = $scope.first + " " + $scope.last;
    $scope.change= function(){
       $scope.full=$scope.first + " " + $scope.last;
    }
});

As you seen above, I am changed/update view page input elements with ng-change directive and create same scope function to namecontroller w.r.t.nameApp Module. Hope its helps you!!!

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.