简体   繁体   English

如何添加到angularjs中的现有对象

[英]How do I add to an existing object in angularjs

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 . 现在在另一个表单上,所有者详细信息,我想只添加到$scope.master I've tried push , concat , but they don't seem to work. 我试过pushconcat ,但它们似乎没有用。 If I use copy again, it just overwrites the existing data. 如果我再次使用copy ,它只会覆盖现有数据。

If you are using AngularJS version >= 1.4.0 then you can use the extend feature like so: 如果您使用的是AngularJS版本> = 1.4.0,则可以使用extend功能,如下所示:

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 . 这会将$scope.ownerDetails (或您获取所有者详细信息的任何位置)中的所有值复制到$scope.master

For Angular less than 1.4.0 you can workaround to inject your own extend method: 对于小于1.4.0的Angular,您可以解决方法来注入自己的extend方法:

// 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);
};

push and concat are for arrays and $scope.master is an object. pushconcat用于数组,$ scope.master是一个对象。 To add another key: 要添加另一个键:

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

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. 但是,如果您提到的“表单”是HTML形式的字段,则可以使用ngmodel将用户详细信息绑定到$ scope.master。 Like: 喜欢:

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

Just another option for you. 只是另一种选择。

All the best. 祝一切顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM