3

I am adding company name from a form to

$scope.master = {};

using

$scope.master = angular.copy(cmpy); 
// This gives me result as Object {cname: "Company 2"}

Now on another form, the owner details and I want to just add to $scope.master. I've tried push, concat, but they don't seem to work. If I use copy again, it just overwrites the existing data.

3 Answers 3

2

If you are using AngularJS version >= 1.4.0 then you can use the extend feature like so:

angular.extend($scope.master, $scope.ownerDetails);

This will copy all the values from $scope.ownerDetails (or whatever place you are getting the owner details) to the $scope.master.

For Angular less than 1.4.0 you can workaround to inject your own extend method:

// This code is basically copied from AngualrJS 1.4.0 library
function baseExtend(dst, objs, deep) {
    var h = dst.$$hashKey;

    for (var i = 0, ii = objs.length; i < ii; ++i) {
        var obj = objs[i];
        if (!angular.isObject(obj) && !angular.isFunction(obj)) {
            continue;
        }

        var keys = Object.keys(obj);
        for (var j = 0, jj = keys.length; j < jj; j++) {
            var key = keys[j];
            var src = obj[key];

            if (deep && angular.isObject(src)) {
                if (angular.isDate(src)) {
                    dst[key] = new Date(src.valueOf());
                } else {
                    if (!angular.isObject(dst[key])) {
                        dst[key] = angular.isArray(src) ? [] : {};
                    }
                    baseExtend(dst[key], [ src ], true);
                }
            } else {
                dst[key] = src;
            }
        }
    }
    if (h) {
        dst.$$hashKey = h;
    } else {
        delete dst.$$hashKey;
    }
    return dst;
}

angular.merge = function(dst) {
    var slice = [].slice;
    return baseExtend(dst, slice.call(arguments, 1), true);
};
Sign up to request clarification or add additional context in comments.

Comments

0

push and concat are for arrays and $scope.master is an object. To add another key:

$scope.master['owner_details'] = owner_details;

1 Comment

as i am using a format such as this - master.firstname, if i use $scope.master['owner_details'], it creates one extra level of key which is not required
0

Other answers are fine to programmatically set the master variable if that's what you want (and you may well due to you trying push etc)

However if the "form" you mention is an HTML form of fields, you can just bind using ngmodel the user details into $scope.master. Like:

<input type="text" ng-model="master.firstname" />

Just another option for you.

All the best.

1 Comment

This would have been fine, but the first form has one input field and when click on subit it calls a function which stores the value in master. The second form calls another function. If I use angular.copy again, it overwrites the value of the first form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.