10

I have a simple question about angularjs one-way-data-binding.

Assume that in same page, we have two input box A and B,

How can they work like:

input A will change input B, but input B will NOT change input A,

I know angular has bindonce, but I want is one-way-data-binding

thanks for your answer..... I tried, but all solutions are failed.........:(

Can we add something like directive to controll it?

3
  • 1
    You can put a watcher on input A's variable and make it set input B's variable whenever it is changed. Commented Jul 11, 2014 at 6:56
  • 1
    Try something before asking questions, If you already have, then add your code Commented Jul 11, 2014 at 6:59
  • possible duplicate of Populate one input field based on other Commented Jul 11, 2014 at 7:07

3 Answers 3

26

You can use ng-value. It will show the model, but not update it. Doesn't require any extra JS wiring.

<input type="text" ng-model="a">
<input type="text" ng-value="a">

DEMO

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

4 Comments

Epic solution. Completely forgot about this directive.
can we use something like directive
@Awakening Not sure what you mean. ng-model and ng-value are directives.
how use ng-change with ng-value?
2

DEMO

HTML

<input type="text" ng-model="a">
<input type="text" ng-model="b">

JS

// Put this code in your controller
$scope.$watch('a', function(newValue, oldValue) {
    if ($scope.b === undefined || newValue !== oldValue) {
        $scope.b = newValue;
    }
});

1 Comment

Can we use something like directive ?
0

Try this:

Html:

<input type="text" ng-model="a" data-ng-change="valueChange(a)">
<input type="text" ng-model="b">

Js:

$scope.valueChange=function(newValue){
     $scope.b = newValue;    
}

1 Comment

Can we use something like directive?

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.