Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am learning AngularJS by building a web application. Have a prototype object in my service module (in the fiddle, I have mocked it within the controller).

Now when I try to update the child element - repeatableInfo - in the fiddle, it's not set. My guess is, in JavaScript objects are passed as reference. When Object.create is used, a new copy of the object is created and all the properties become prototype properties?

    var apiData={};
apiData.formObjectProto = {
    'commonInfo': '',
    'repeatableInfo': [
        {'aInfo': ''}
     ]
};

$scope.mainObj = Object.create(apiData.formObjectProto);

When it's replaced as below, everything works.

 var apiData={};
apiData.formObjectProto = {
    'commonInfo': '',
    'repeatableInfo': [
        {'aInfo': ''}
     ]
};

//$scope.mainObj = Object.create(apiData.formObjectProto);
$scope.mainObj = {
    'commonInfo': '',
    'repeatableInfo': [
        {'aInfo': ''}
     ]
};

So my question, can we not used OOP concept in angular? Or there is something I am missing. Please help

JS Fiddle

Update 1

Using the below helps. So, Object.create() is not recommended, but rather use angular.copy() to create a clone of prototype object?

$scope.mainObj = angular.copy(apiData.formObjectProto);
share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.