Empty ng-model overwrites ng-value

when I use ng-model together ng-value. ng-value returns empty even there is a value given. for example, there are two controllers called FirstCtrl and SecondCtrl. This is FirstCtrl, and If I type a value for this input, it will write in my SeconCtrl input since I call ng-value="firstname" in my SecondController:

I can pass the value from firstctrl to secondctrl(I mean first input field to second input field). But the second input field has an ng-model called d_firstname, which doesn't allow to save ng-value but ng-value is displaying the correct value but saves empty because of ng-model(d_firstname). However, i need d_firstname if user wants to enter new value. Moreover, If I type over the input field in the secondCtrl, I can save the record because of d_firstname but if I keep not modifying anything in that input field, returns empty. hope you get the problem. Please someone help on this.. I wanted to pass data from one form to other if checkbox clicked for billing details and shopping details are same

FirstController input:

<input type="text" name="firstname" ng-model="firstname" required>

SecondCtrl input:

<input name="firstname" ng-value="firstname" ng-model="d_firstname">

Upvotes: 1

Views: 985

Answers (1)

charlietfl
charlietfl

Reputation: 171679

There is no point in using ng-value and ng-model together.

You could use ng-change event to update the second one

<input name="firstname" ng-model="firstname" ng-change="updateName()" required>
<input name="d_firstname" ng-model="d_firstname">

Controller:

$scope.updateName = function(){
    $scope.d_firstname = $scope.firstname
}

Note that you should always use an object in ng-model. You will run into problems with child scopes if you don't

Upvotes: 1

Related Questions