2

I want to bind a $scope.variable to another $scope.variable like this:

$scope.b = '';
$scope.a = $scope.b;
$scope.b = 10;
console.log($scope.a);// It gives blank value

In other words, i want to assign $scope.b value to $scope.a whenever $scope.b get a value.

4
  • You could add a watcher to $scope.b and update $scope.a in the callback. Commented Mar 18, 2016 at 9:10
  • Yeah we can add $watch and assign value in the callback but i have around 200 variables which are depending on each other. Do you have any global solution for this? Commented Mar 18, 2016 at 9:15
  • '..200 variables which are depending on each other..' that just sounds like a minefield. Are you able to give any more context to your set up? Commented Mar 18, 2016 at 9:17
  • Suppose i am building a web like excel sheet in which lots of fields are depending on each other values in their calculations Commented Mar 18, 2016 at 9:20

5 Answers 5

6

In JavaScript you can copy reference to the obj and it will give you what you expect.

You need to do this:

$scope.b = {prop: 0};
$scope.a = $scope.b;
$scope.b.prop = 10;
console.log($scope.a.prop);// It gives 10
Sign up to request clarification or add additional context in comments.

Comments

3

You must do a watch:

$scope.$watch('b', function (newValue) {
    $scope.a = newValue;
});

$scope.b = 2;
Console.log($scope.a); // Careful! This will still be undefined, explanation below.

Angular has a digest loop that it executes to process bindings. If you want you can do a $scope.$apply() to instruct angular to run the loop or you can just leave it to do its thing, that's probably what you want in real code.

6 Comments

Yeah we can add $watch and assign value in the callback but i have around 200 variables which are depending on each other. Do you have any global solution for this?
Perhaps you can tell us more about your use case? 200 variables depending on each other sounds weird.
Yeah, i can understand lots of variable depending on each other sounds weird, but i have to develop a realtime web page just like the excel sheet in which lots of columns are depending on each other in their calculations
Alright, so it's like any variable can have more than one dependent while also depending on other variables?
Exactly, same like excel sheet
|
2

$watch will be called everytime $scope.a changes. If you have multiple data, then you can create a JSON instead of individual variables

$scope.tmp = {
  'b' : 22,
  'a' : 33
};
$scope.$watch('tmp', function() {
   $scope.tmp.a = $scope.tmp.b; // Set $scope.a here
}, true);

1 Comment

When watching objects you should pass true as the third parameter of $watch.
1

You can use Object.defineProperty() to define one property in terms of another:

function myController($scope) {
  Object.defineProperty($scope, 'b', {
    get: function() {
      return $scope.a;
    },
    set: function(value) {
      $scope.a = value;
    }
  });
  
  $scope.b = 10;
  console.log($scope.a);
  $scope.a = 20;
  console.log($scope.b);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <div ng-controller="myController">
    <input type="text" ng-model="a" />
    <input type="text" ng-model="b" />
  </div>
</div>

3 Comments

Suppose i need to bind 100 of $scope.variables to each other, then? do i need to add this function for each variable or is there any global way?
i want to develop a realtime web page just like the excel sheet in which lots of columns are depending on each other in their calculations
This worked well for my case. I had a string like "Hello, [name1] & [name2]" and the name variables would change with a textbox.
0

In javascript you can copy objects references, As what you are doing is copying the value of b to a even if you want to copy the reference. Because $scope.b is primitive type data. If you want to copy its reference it should be of non-primitive type. as,

$scope.b = {c:''};
$scope.a = $scope.b;
$scope.a.c = 10;
console.log($scope.b.c); //output 10

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.