javascript - Force one way binding while assigning variable to another variable -


i'm trying assign variable variable , try 1 way binding. when value updated in view, updates original variable too. how stop 2 way binding while assigning variable another.

for example:

function personcontroller ($scope) {     var templatevalue= "original value";      $scope.myval= templatevalue; } 

in view:

<input type="text" ng-model="myval" /> 

result:

when type in textbox, updates value in myval , templatevalue too, i.e value in templatevalue changes whatever typed in input box. there way assign variable variable doing 1 way binding? want 2 way binding between $scope.myval , input box not between templatevalue , input box.

you can't "force one-way binding" because of weay javascript works.

in example, updating myval not update templatevalue.

function personcontroller($scope) {     var templatevalue = "original value";     $scope.myval = templatevalue; } 

if have following structure, yes, changing myval.test update templatevalue.test because both reference same object in memory.

function personcontroller($scope) {     var templatevalue = { test: "original value" };     $scope.myval = templatevalue; } 

if want myval , templatevalue reference different objects have same content, make copy of original object:

$scope.myval = angular.copy(templatevalue); 

i suggest familiarising javascript reference vs. value.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -