Alexandru Rada Alexandru Rada - 1 year ago 64
AngularJS Question

How to create angular models from same prototype without binding?

I have a small issue. I have an object which I want to use to create two angular models without binding, based on initial object as prototype:

var def = {
value: 'example'

in Angular:

var c = Object.create(def);
c.anothervalue = 12345;
$scope.c = c;

var d = Object.create(def);
d.anothervalue = 765432;
$scope.d = d;

c and d are created correctly, without binding. But I can't access initial value of the object in Angular View, even if in the controller it works good.

I read about and is something with Object.prototype but I couldn't find any solutions.

I've put a fiddle here

Answer Source

Change your copy function from Object.create to angular.copy:

var c = angular.copy(def);

I tried it in your Fiddle, and I think it does what you want:

c:{ "value": "example", "anothervalue": 12345 } 
d:{ "value": "example", "anothervalue": 765432 }