Reputation: 6868
Below is my object:
$scope.obj = {
name: null,
child: { name : name}
};
<input type="text" ng-model="obj.name" />
What I am trying to do is when I will have latest value in my obj.name property it should automatically get reflected in obj.child.name property.
I would always like to use updated value and everywhere I am using obj.child.name property only so later on if I forgot to assign like below:
$scope.obj.child.name = $scope.obj.name
then it will always use old value and I don't want to do this at so many place. So I want this process to be automated like whenever name $scope.obj.name will be updated; it should automatically update $scope.obj.child.name.
But with above code I am getting error: name is undefined.
Why is the above declaration not possible and how to achieve this?
Upvotes: 1
Views: 94
Reputation: 32145
You are getting name
is undefined
because JavaScript is trying to find a name
object in the global scope and can't find it, in this code:
child: { name : name//undefined}
This is how you can define it:
$scope.obj = {
name: "Ay"
};
$scope.obj.child = {
name: $scope.obj.name
}
We will refer to the $scope.obj.name
in the child
property declaration here so it's correctly binded.
Upvotes: 1
Reputation: 5957
You're declaring obj wrong:
$scope obj = {
name: null,
child: { name : name}
};
Should be
$scope.obj = {
name: null,
child: { name : name}
};
Upvotes: 0