Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
179
rated 0 times [  181] [ 2]  / answers: 1 / hits: 15374  / 9 Years ago, wed, october 14, 2015, 12:00:00

I have created a custom directive which has two values. first is config object and second is data object. I modify this config and data objects inside my directive which is reflecting it in parent scope. Which is causing me error when I have to use directive multiple times.



I followed https://umur.io/angularjs-directives-using-isolated-scope-with-attributes/ and I am using isolated scope.



I want one way data binding for objects in isolated scope. Whatever I change in directive function it should not reflect in the parent scope.



below is scope of directive.



scope: {
config: &config,
dataObj: &dataObj
}


here is how I access it in the link function of directive



var config = scope.config();
var dataObj= scope.dataObj();


I am assuming that pass by reference is happening here.



I am adding JSbin. check the console the value of object is changing and reflecting in parent scope.



https://jsbin.com/vagowe/edit?html,js,output


More From » angularjs

 Answers
77

passing text is one-way binding(@) and passing object is two-way binding(=)



passing object as text



<custom-directive config={{config}}></custom-directive>


scope in directive



scope: {
config: @
}


converting the string back to object in link



var config = angular.fromJson(scope.config);

[#64745] Monday, October 12, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
felixa

Total Points: 180
Total Questions: 113
Total Answers: 108

Location: Palau
Member since Sat, Aug 21, 2021
3 Years ago
;