Component

https://docs.angularjs.org/guide/component

component本质上就是directive。

This is a shorthand for registering a special type of directive, which represents a self-contained UI component in your application. Such components are always isolated (i.e. scope: {}) and are always restricted to elements (i.e. restrict: 'E').

Component definitions are very simple and do not require as much configuration as defining general directives. Component definitions usually consist only of a template and a controller backing it.

MVC的本质就是将controller和template关联起来,controller将数据显示到template中。component将这种关联做的更好。

When not to use Components:

  1. for directives that need to perform actions in compile and pre-link functions, because they aren't available
  2. when you need advanced directive definition options like priority, terminal, multi-element
  3. when you want a directive that is triggered by an attribute or CSS class, rather than an element

component解决了什么问题:

  1. app中的某个部分如何重用
  2. app中各部分之间的scope不是隔离的
  3. component可以自由的重复使用
  4. index.html被简化了,细节的实现都分离出来了,提高了可维护性
  5. component是隔离的,不受外界影响,同时也不会影响app中的其他部分
  6. 可以独立的测试我们的component

Comparison between Directive definition and Component definition

component使用

  1. 文件名建议:xxxComponent.component.js
  2. component名字规则和directive一样,定义的时候使用驼峰样式(myAwesomeComponent),模板中使用的时候使用-分割(my-awesome-component)
  3. template中使用controller的实例$ctrl替代scope来访问数据,这个实例别名可以自定义 - 使用controllerAs属性
  4. controller可以使用controller()注册的controller,也可以使用行内定义的方式
<html ng-app="phonecatApp">
<head>
...
<script src="bower_components/angular/angular.js"></script>
<script src="app.js"></script>
<script src="phone-list.component.js"></script>
</head>
<body> <!-- Use a custom component to render a list of phones -->
<phone-list></phone-list> </body>
</html>
// Define the `phonecatApp` module
angular.module('phonecatApp', []); // Register `phoneList` component, along with its associated controller and template
angular.
module('phonecatApp').
component('phoneList', {
template:
'<ul>' +
'<li ng-repeat="phone in $ctrl.phones">' +
'<span>{{phone.name}}</span>' +
'<p>{{phone.snippet}}</p>' +
'</li>' +
'</ul>',
controller: function PhoneListController() {
this.phones = [
{
name: 'Nexus S',
snippet: 'Fast just got faster with Nexus S.'
}, {
name: 'Motorola XOOM™ with Wi-Fi',
snippet: 'The Next, Next Generation tablet.'
}, {
name: 'MOTOROLA XOOM™',
snippet: 'The Next, Next Generation tablet.'
}
];
},
controllerAs: 'ctrl'
});

example

var myMod = angular.module(...);
myMod.component('myComp', {
template: '<div>My name is {{$ctrl.name}}</div>',
controller: function() {
this.name = 'shahar';
}
}); myMod.component('myComp', {
template: '<div>My name is {{$ctrl.name}}</div>',
bindings: {name: '@'}
}); myMod.component('myComp', {
templateUrl: 'views/my-comp.html',
controller: 'MyCtrl',
controllerAs: 'ctrl',
bindings: {name: '@'}
});

API

component(name, options) returns ng.$compileProvider

https://docs.angularjs.org/api/ng/service/$compile#directive-definition-object

bindings

这个就是类似于directive中的scope属性, 支持:

  1. @ 字符值 - input
  2. < 单向绑定 - input
  3. & 回调传递 - 父组件的方法,用于向上的交互 - output
  4. = 双向绑定 - 设计原则中不推荐使用
angular.module('docsIsolateScopeDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
$scope.igor = { name: 'Igor', address: '123 Somewhere' };
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
scope: {
customerInfo: '=info'
},
templateUrl: 'my-customer-iso.html'
};
});
    .component('qqtrMenu',{
templateUrl:'/secu/components/menu/menu.component.html',
bindings: {
menuitems: "<"
},
controller: 'menuCompCtrl',
})

交互式组件实例

https://plnkr.co/edit/?p=preview

index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-component-tabs-pane-production</title> <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="script.js"></script> </head>
<body ng-app="docsTabsExample">
<my-tabs>
<my-pane title="Hello">
<h4>Hello</h4>
<p>Lorem ipsum dolor sit amet</p>
</my-pane>
<my-pane title="World">
<h4>World</h4>
<em>Mauris elementum elementum enim at suscipit.</em>
<p><a href ng-click="i = i + 1">counter: {{i || 0}}</a></p>
</my-pane>
</my-tabs>
</body>
</html> <!--
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->

script.js

(function(angular) {
'use strict';
angular.module('docsTabsExample', [])
.component('myTabs', {
transclude: true,
controller: function MyTabsController() {
var panes = this.panes = [];
this.select = function(pane) {
angular.forEach(panes, function(pane) {
pane.selected = false;
});
pane.selected = true;
};
this.addPane = function(pane) {
if (panes.length === 0) {
this.select(pane);
}
panes.push(pane);
};
},
templateUrl: 'my-tabs.html'
})
.component('myPane', {
transclude: true,
require: {
tabsCtrl: '^myTabs'
},
bindings: {
title: '@'
},
controller: function() {
this.$onInit = function() {
this.tabsCtrl.addPane(this);
console.log(this);
};
},
templateUrl: 'my-pane.html'
});
})(window.angular); /*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/

my-pane.html

<div class="tab-pane" ng-show="$ctrl.selected" ng-transclude></div>

<!--
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->

my-tabs.html

<div class="tabbable">
<ul class="nav nav-tabs">
<li ng-repeat="pane in $ctrl.panes" ng-class="{active:pane.selected}">
<a href="" ng-click="$ctrl.select(pane)">{{pane.title}}</a>
</li>
</ul>
<div class="tab-content" ng-transclude></div>
</div> <!--
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->

基于组件的开发

https://docs.angularjs.org/guide/component

一个应用应该是一个组件数,每个组件只控制自己的视图和数据. bindings只用字符和单向绑定,数据单向流入; 使用&绑定父组件的方法,实现向上交互

推荐和参考:

https://docs.angularjs.org/tutorial/step_03

https://docs.angularjs.org/api/ng/provider/$compileProvider#component

In order to retrieve and instantiate a component's controller, AngularJS provides the $componentController service.

angularjs component的更多相关文章

  1. 一篇文章看懂angularjs component组件

     壹 ❀ 引 我在 angularjs 一篇文章看懂自定义指令directive 一文中详细介绍了directive基本用法与完整属性介绍.directive是个很神奇的存在,你可以不设置templa ...

  2. angularjs中directive指令与component组件有什么区别?

     壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...

  3. Using RequireJS in AngularJS Applications

    http://www.sitepoint.com/using-requirejs-AngularJS-applications/ While writing large JavaScript appl ...

  4. 升级 AngularJS 至 Angular

    Victor Savkin 大神撰写了一系列文章详细介绍如何升级 AngularJS 应用: NgUpgrade in Depth Upgrade Shell Two Approaches to Up ...

  5. 了解angularjs中的生命周期钩子函数$onInit,$onChange,$onDestory,$postLink

     壹 ❀ 引 我在前面花了三篇文章用于介绍angularjs的指令directive,组件component,并专门花了一篇文章介绍directive与component的不同,其中提到在compon ...

  6. angularjs 一篇文章看懂自定义指令directive

     壹 ❀ 引 在angularjs开发中,指令的使用是无处无在的,我们习惯使用指令来拓展HTML:那么如何理解指令呢,你可以把它理解成在DOM元素上运行的函数,它可以帮助我们拓展DOM元素的功能.比如 ...

  7. angular directive 深入理解

    由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解. 感觉才开始真正理解了这句话的意思: In ...

  8. angularjs directive and component 指令与组件 ( 1.5.0 以后 )

    之前写过一篇了 http://www.cnblogs.com/keatkeat/p/3903673.html 但某些部分写的不太清楚,甚至有点错误,所以今天特地在这里再来谈谈. 这篇主要是说指令的隔离 ...

  9. AngularJs学习笔记--Understanding the Model Component

    原版地址:http://docs.angularjs.org/guide/dev_guide.mvc.understanding_model 在angular文档讨论的上下文中,术语“model”可以 ...

随机推荐

  1. python测试开发django-12.models设置主键primary_key

    前言 django的models新增数据库表时,如果不设置主键,会默认新增一个id为主键,如果我们想自己设置一个字段为主键,需加个参数primary_key=True 默认id主键 新增一张用户表,表 ...

  2. Unity Shader _Time 的单位

    名称 类型 说明 _Time float4 t 是自该场景加载开始所经过的时间,4个分量分别是 (t/20, t, t*2, t*3) _SinTime float4 t 是时间的正弦值,4个分量分别 ...

  3. ArrayList 排序方法的性能对比

    20000=>ZXP 二分法 getSeriesMinSort2(list) Time is 67000 20000=>循环 getSeriesMinSortFor(list) Time ...

  4. Android Activity启动流程源码全解析(1)

    前言 Activity是Android四大组件的老大,我们对它的生命周期方法调用顺序都烂熟于心了,可是这些生命周期方法到底是怎么调用的呢?在启动它的时候会用到startActivty这个方法,但是这个 ...

  5. Asp.Net Mvc Action过滤器(二)

    在Mvc中为Action添加过滤器,有两种方式, 一.使用ActionFilterAttribute,简单方式,同时支持Result的过滤处理, 1.可以为空,支持的重写:OnActionExecut ...

  6. Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)

    在Java中,实现定时任务有多种方式,本文介绍4种,Timer和TimerTask.Spring.QuartZ.Linux Cron. 以上4种实现定时任务的方式,Timer是最简单的,不需要任何框架 ...

  7. 【.Net】 C#访问修饰符

    一 类的修饰符:  C#中类的默认修饰符是internal.1 private 只有对包.NET中的应用程序或库才能访问.2 public 不限制对类的访问. 3 protected 只可以被本类和其 ...

  8. VNC XEN 双鼠标问题 以及 使用 virt-manager 工具创建的 Xen 虚拟机配置文件不在 /etc/xen/ 目录中了

    0.本人用的是Ubuntu 12.04,在其中安装xen 4.1,用的是virt-manager安装虚拟机 1.VNC XEN 双鼠标问题,在配置文件中加入: 找到:(usb 1),在之后加入: (u ...

  9. [转]教你修复win7中复制粘贴失效的问题

    教你修复win7中复制粘贴失效的问题 发布时间:2018-01-17             使用win7系统的时候,我们经常需要对立面的内容进行复制粘贴来引用一些网站的内容,不过最近有网友在使用这个 ...

  10. C++11 std::shared_ptr总结与使用

    最近看代码,智能指针用的比较多,自己平时用的少,周末自己总结总结.方便后续使用. std::shared_ptr大概总结有以下几点: (1) 智能指针主要的用途就是方便资源的管理,自动释放没有指针引用 ...