1

Here in this code i have used two dynamic variables fbid and fburl. fburl is a expression using fbid.

$scope.fburl = "https://graph.facebook.com/"+$scope.fbid+"/picture?type=normal";

Here is my code.

// Code goes here
angular.module("testApp", [])
  .controller("testCtrl", function($scope) {
    $scope.fbid = "bennadel";
    $scope.fburl = "https://graph.facebook.com/"+$scope.fbid+"/picture?type=normal";
    console.log($scope.fburl);
  })
<!DOCTYPE html>
<html ng-app="testApp">

  <head>
    <script data-require="angular.js@*" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script>
    <link href="style.css" rel="stylesheet" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="testCtrl">
    FacebookID<input type="text" ng-model="fbid" >
    <img ng-src= "{{fburl}}"/>
   <div>{{fburl}}</div>
  </body>

</html>

Now my question is when I am updating the fbid why fburl is not updating automatically ?

1
  • Angular not watch for setted values, you must say it.Using $scope.$watch Commented Feb 23, 2015 at 13:04

2 Answers 2

2

It's because the value can't update after the fact, you need to add a watcher on the variable fbid.

AngularJS docuemntation for $watch

// Code goes here
angular.module("testApp", [])
  .controller("testCtrl", function($scope) {
    $scope.fbid = "bennadel";
    $scope.fburl = "";
    $scope.$watch('fbid ', function(newValue, oldValue) {
        $scope.fburl = "https://graph.facebook.com/"+newValue+"/picture?type=normal";
        console.log($scope.fburl);
    });
  })
<!DOCTYPE html>
<html ng-app="testApp">

  <head>
    <script data-require="angular.js@*" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script>
    <link href="style.css" rel="stylesheet" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="testCtrl">
    FacebookID<input type="text" ng-model="fbid" >
    <img ng-src= "{{fburl}}"/>
   <div ng-bind="fburl"></div>
  </body>

</html>

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

Comments

2

Alternate solution - cleaner way.

// Code goes here
angular.module("testApp", [])
  .controller("testCtrl", function($scope) {
    $scope.fbid = "bennadel";
$scope.fbFn = function() {
                return "https://graph.facebook.com/"+$scope.fbid+"/picture?type=normal";
            };
  })
<!DOCTYPE html>
<html ng-app="testApp">

  <head>
    <script data-require="angular.js@*" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script>
    <link href="style.css" rel="stylesheet" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="testCtrl">
    FacebookID<input type="text" ng-model="fbid" >
<img ng-src= "{{fbFn()}}"/>
    <div >{{fbFn()}}</div>
  </body>

</html>

Happy Helping!

2 Comments

When you assigned a function to fbFn its work, but why its not working when I directly assign a the expression value to the variable?
when there is change in $scope.anyPropery ($scope.fbid in this case) $digest is run which causes all $scope-associated functions also run, that's why 'fbFn' is working here. I like this approach compared to setting $watch. You can understand more about $diget cycle working

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.