Saturday, 15 February 2014

prototype - Angularjs create scope object using Object.create -



prototype - Angularjs create scope object using Object.create -

i learning angularjs building web application. have prototype object in service module (in fiddle, have mocked within controller).

now when seek update kid element - repeatableinfo - in fiddle, it's not set. guess is, in javascript objects passed reference. when object.create used, new re-create of object created , properties become prototype properties?

var apidata={}; apidata.formobjectproto = { 'commoninfo': '', 'repeatableinfo': [ {'ainfo': ''} ] }; $scope.mainobj = object.create(apidata.formobjectproto);

when it's replaced below, works.

var apidata={}; apidata.formobjectproto = { 'commoninfo': '', 'repeatableinfo': [ {'ainfo': ''} ] }; //$scope.mainobj = object.create(apidata.formobjectproto); $scope.mainobj = { 'commoninfo': '', 'repeatableinfo': [ {'ainfo': ''} ] };

so question, can not used oop concept in angular? or there missing. please help

js fiddle

update 1

using below helps. so, object.create() not recommended, rather utilize angular.copy() create clone of prototype object?

$scope.mainobj = angular.copy(apidata.formobjectproto);

i think improve conform angular's set of design pattern different approach should used able new new instance of map on each injection.

var deps, appmodule; deps = []; appmodule = angular.module('myapp', deps); appmodule.factory('myappfactory', function() { var mill = {}; factory.formobjectproto = { 'commoninfo': '', 'repeatableinfo': [ {'ainfo': ''} ] }; homecoming factory; }); appmodule.controller('myappctrl', function ($scope, myappfactory) { $scope.mainobj = myappfactory; console.log('myappctrl# $scope.mainobj is: ' + json.stringify($scope.mainobj)); }); appmodule.controller('oneinfoctrl', function ($scope) { $scope.$watch('oneinfo.ainfo', function (newvalue, oldvalue){ console.info('oneinfoctrl watch# new value of oneinfo.ainfo is: ' + newvalue); }); $scope.$watch('mainobj.commoninfo', function (newvalue, oldvalue){ console.info('mainobj.commoninfo watch# new value of commoninfo (present in $parent) is: ' + newvalue); }); });

this accomplish you're doing in update conforming more closely oop principles.

angularjs prototype angularjs-scope

No comments:

Post a Comment