javascript - How to extend default options with Angular? - Stack Overflow

I need to extend default values with options in angular way:var defaultOptions = {a: 'a'},opt

I need to extend default values with options in angular way:

var defaultOptions = {
      a: 'a'
    },
    options = {
      b: 'b'
    };
var result = $.extend({}, defaultOptions, options);

Please notice, that result is a new object, because I want to preserve both of the original objects defaultOptions and options.

I'm new in Angular, could you please help me to do the same result with angular in the most elegant way?

EDIT:

I have already found angular.extend() but I can't figure out a way how to preserve defaultOptions and options.

I know that I can do it this way:

var result = angular.extend({}, defaultOptions);
angular.extend(result, options);

But it doesn't look very elegant.

I need to extend default values with options in angular way:

var defaultOptions = {
      a: 'a'
    },
    options = {
      b: 'b'
    };
var result = $.extend({}, defaultOptions, options);

Please notice, that result is a new object, because I want to preserve both of the original objects defaultOptions and options.

I'm new in Angular, could you please help me to do the same result with angular in the most elegant way?

EDIT:

I have already found angular.extend() but I can't figure out a way how to preserve defaultOptions and options.

I know that I can do it this way:

var result = angular.extend({}, defaultOptions);
angular.extend(result, options);

But it doesn't look very elegant.

Share Improve this question edited Sep 15, 2014 at 7:23 Warlock asked Sep 15, 2014 at 7:12 WarlockWarlock 7,48110 gold badges56 silver badges75 bronze badges 2
  • @Satpal I want to preserve both of the original objects, defaultOptions and options. – Warlock Commented Sep 15, 2014 at 7:16
  • 1 @Satpal I need to do the same with Angular, not jQuery. – Warlock Commented Sep 15, 2014 at 7:18
Add a ment  | 

3 Answers 3

Reset to default 5

You need to use angular.extend

Extends the destination object dst by copying all of the properties from the src object(s) to dst. You can specify multiple src objects.

Usage

var result = angular.extend({}, defaultOptions, options)

Angular provides an extend function.

var result = angular.extend({},defaultOptions, options)

you can use angular copy

var result = angular.copy(defaultOptions );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636473a4637425.html

相关推荐

  • javascript - How to extend default options with Angular? - Stack Overflow

    I need to extend default values with options in angular way:var defaultOptions = {a: 'a'},opt

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信