Components that you use across multiple applications need to follow a module pattern that keeps them separate from your application logic. This will allow you to make these Angular 2 components reusable and shareable and is the same pattern followed by many libraries that you may import into your projects.

The structure likes this:

In widget-one.component.html. we use *ngIf to control the display, to do this, we have to import CommonModule from angular/common, which inlcudes NgIf, NgFor....

import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import {WidgetOneComponent} from './widget-one.component';
import {WidgetTwoComponent} from './widget-two.component'; @NgModule({
imports: [CommonModule],
declarations: [WidgetOneComponent, WidgetTwoComponent],
exports: [WidgetOneComponent, WidgetTwoComponent, CommonModule]
})
export class WidgetsModule { }

The CommonModule is available for root Module. When you create a sub module, it won't import CommonModule by default, so you need to imports it and exprots it to other sub module for free.

widget-one.component.ts:

import { Component, OnInit } from '@angular/core';

@Component({
moduleId: module.id,
selector: 'widget-one',
templateUrl: '<div *ngIf=selected'This is widget one </div>
})
export class WidgetOneComponent implements OnInit {
selected = false;
constructor() { } ngOnInit() { } }

[Angular 2] Create Shareable Angular 2 Components的更多相关文章

  1. Angular学习笔记:Angular CLI

    定义 Angular CLI:The Angular CLI is a command line interface tool that can create a project, add files ...

  2. angular enter事件,angular回车事件

    angular回车键搜索,angular enter搜索 对于搜索框,用户在输入内容后的搜索习惯不是鼠标点击搜索按钮,而是直接按enter键,那我们怎么给enter键绑定事件呢,其实很简单,代码如下: ...

  3. Angular 2 升级到 Angular 5

    Angular 2 升级到 Angular 5 ts文件最上面的import语句里不要添加 .ts 后缀 , 不然 npm start 编译会失败 . 虽然浏览器能打开项目的URL , 但是内容会丢失 ...

  4. [React] Create and import React components with Markdown using MDXC

    In this lesson I demonstrate how to use the library MDXC to create and import React components with ...

  5. Angular系列一:Angular程序架构

    Angular程序架构 Angular程序架构 组件:一段带有业务逻辑和数据的Html服务:用来封装可重用的业务逻辑指令:允许你向Html元素添加自定义行为模块: 环境搭建 安装nodeJs安装好no ...

  6. [Angular 2] Create Angular 2 Porject with Angular CLI

    Install: npm i -g angular-cli Create a project: ng new hello-angular2 Run the project: cd hello-angu ...

  7. [Angular] Design API for show / hide components based on Auth

    Simple Auth service: import { Injectable } from '@angular/core'; import {HttpClient} from '@angular/ ...

  8. [Angular Directive] Create a Template Storage Service in Angular 2

    You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. Y ...

  9. [Angular 2] Create template with Params

    Angular 2 templates have a special let syntax that allows you to define and pass a context when they ...

随机推荐

  1. Storm的本地运行模式示例

    以word count为例,本地化运行模式(不需要安装zookeeper.storm集群),maven工程, pom.xml文件如下: <project xmlns="http://m ...

  2. Int.Parse()、Convert.toInt32()和(int)区别

    通过网上的查询从而了解了Int.Parse().Convert.toInt32()和(int)区别. 一.定义上的差别 int类型表示一种整型,.NET Framework 类型为 System.In ...

  3. 在PC上测试移动端网站和模拟手机浏览器的5大方法

    在PC上测试移动端网站和模拟手机浏览器的5大方法 来源:互联网 作者:佚名 时间:03-19 10:14:54 [大 中 小]                最近公司要开发网站的移动版,让我准备准备知 ...

  4. duilib修复ActiveXUI控件bug,以支持flash透明动态背景

    转载请说明原出处,谢谢~~ 昨天在QQ控件里和同学说起QQ2013登陆窗体的开发,从界面角度考虑,单单一个登陆界面是很容易做出来的.腾讯公司为了 防止各种盗号行为可谓煞费苦心,QQ2013采用了动态背 ...

  5. C++ 编程输入输出语句

    C++ 的标准输入.输出就是我们已经使用的包含头文件iostream,他不但提供了I/O的库函数,也提供了使用该库的流模式,从cin>> 流入  和cout<<流出到设备就是一 ...

  6. g++安装 Gnome/Gtk+开发库 的 环境安装(安装widgets的必要条件)

    sudo yum install gcc-c++ 我在更新软件源后,看了看最新版的GTK开发版是3.0的. 所以,基本可以照搬. sudo yum -t install gtk3 sudo yum - ...

  7. 《GettingThingsDone》--GTD学习笔记(二)-GTD实践指导

    一.准备阶段的建议: 高级的工作管理方法就是学会一套系统并付诸实施,直到将这套系统和方法融入你的工作和生活中. 通过行动使自己感觉良好,要比通过使自己感觉良好而进入一种较佳的行动状态容易的多. 当你在 ...

  8. Chapter 1 初探Caffe

    首先下载windows下源码: Microsoft 官方:GitHub - Microsoft/caffe: Caffe on both Linux and Windows 官方源码使用Visual ...

  9. [转] GCC 中的编译器堆栈保护技术

    以堆栈溢出为代表的缓冲区溢出已成为最为普遍的安全漏洞.由此引发的安全问题比比皆是.早在 1988 年,美国康奈尔大学的计算机科学系研究生莫里斯 (Morris) 利用 UNIX fingered 程序 ...

  10. hdu 1689 Just a Hook

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    ...