【问题标题】:Angular: default config for componentAngular:组件的默认配置
【发布时间】:2015-03-06 07:16:30
【问题描述】:

如何将此配置应用到我的组件。我使用的是我自己设置的控制器,但是我怎样才能像在 jquery 插件中一样提供默认的配置选项集:

    $.fn.helloWorld = function( options ) {

        // Establish our default settings
        var settings = $.extend({
            descriptionTemplate.content: 'Hello, World!',
            type         : 'tab',
            id           : null
        }, options);

    }

我的控制器正在提供这个对象:

vm.tabs = [
    {
        id: '1',
        type: 'tab',
        sections: {
            tabTitle: 'The Caribbean'
        },
        descriptionTemplate : {
            content: 'text goes here'
        },
        active: true
    }
];

【问题讨论】:

  • 您应该创建一个相应的提供程序并进行配置。来自documentation:“只有当您想要公开一个必须在应用程序启动之前进行的应用程序范围配置的 API 时,您才应该使用 Provider 配方。”

标签: angularjs tabs angularjs-scope angular-ui-bootstrap


【解决方案1】:

您可以在您的情况下使用 angular.config 用于配置目的

常数

var app = angular.module('app',[])
.config('constants', {
   id: 1,
   tab: 'test',
   description: 'test'
});

现在您创建了一个随处可用的配置,您还可以使用 angular.extend({}, object1, object2) 在任何地方扩展它

控制器

app .controller('Ctrl',fuction($scope, constants){
   //you can further extend constants using extend method
   angular.extend(constants,{
       id: 2,
       tab: 'test1',
       description: 'test1'
   });
});

希望对你有帮助,谢谢。

【讨论】:

  • 谢谢@pankajparkar。一个非常干净的解释。
  • 很高兴听到它对您有所帮助,谢谢 :)
猜你喜欢
  • 1970-01-01
  • 2020-03-12
  • 2017-05-18
  • 2021-02-01
  • 1970-01-01
  • 2017-03-27
  • 2013-03-18
  • 2012-12-21
  • 2017-02-17
相关资源
最近更新 更多