Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs one-way-data-binding, can angular do it?

Tags:

angularjs

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?

like image 942
Awakening Avatar asked Jul 11 '14 06:07

Awakening


2 Answers

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

like image 153
Mosho Avatar answered Nov 03 '22 02:11

Mosho


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;
    }
});
like image 30
Miraage Avatar answered Nov 03 '22 02:11

Miraage