[Angular 2] Exposing component properties to the template

Showing you how you can expose properties on your Controller to access them using #refs inside of your template.

// letterSelect.ts

import {Component, View, FORM_DIRECTIVES, NgFor} from ‘angular2/angular2‘;

@Component({
    selector: ‘letter-select‘
})
@View({
    directives: [NgFor,FORM_DIRECTIVES],
    template: `
        <select [(ng-model)]="selectedLetter">
            <option *ng-for="#letter of letters">{{letter}}</option>
        </select>
    `
})

export class LetterSelect {
    letters: string[] = [‘e‘, ‘s‘, ‘w‘];
    selectedLetter: string = ‘e‘;
    constructor() {

    }
}

todoList.ts

import {Component, View, NgFor, FORM_DIRECTIVES} from ‘angular2/angular2‘;
import {TodoService} from ‘./todoService‘;
import {TodoItemRender} from ‘./todoItemRender‘;
import {StartsWith} from ‘./startsWith‘;
import {LetterSelect} from ‘./letterSelect‘;

@Component({
    selector: ‘todo-list‘
})
@View({
    pipes: [StartsWith],
    directives: [NgFor, TodoItemRender, FORM_DIRECTIVES, LetterSelect],
    template: `
          <div>
                <todo-item-render
                    *ng-for="#todo of todoService.todos | startsWith:‘title‘:letterSelect.selectedLetter"
                    [todoinput]="todo"
                >
                </todo-item-render>
                <letter-select #letter-select></letter-select>
          </div>
    `
})

export class TodoList{
    constructor(
        public todoService:TodoService
    ){

    }
}
时间: 2025-01-14 15:45:43

[Angular 2] Exposing component properties to the template的相关文章

[AngularJS] Exploring the Angular 1.5 .component() method

Angualr 1.4: .directive('counter', function counter() { return { scope: {}, restrict: 'EA', transclude: true, bindToController: { count: '=' }, controller: function () { function increment() { this.count++; } function decrement() { this.count--; } th

ES6, Angular,React和ABAP中的String Template(字符串模板)

String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专门的网站介绍ES6入门,还出了一本书: <ECMAScript6标准入门>. http://es6.ruanyifeng.com/ 我们来看看ES6里的String Template. 首先看下面这段代码. <html> <div id="JerryTest"

[Angular 2] Component relative paths

Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component({ selector : 'contacts-header', templateUrl: './header.component.html', styleUrls : ['./header.component.css'] }) export class HeaderComponent implement

angular组件component无法传递数据的坑

问题:使用组件component时组件无法从外获取到绑定的数据 先贴上官方component的介绍及demo地址:https://docs.angularjs.org/guide/component; 将confirm-dialog.html插入到main.html页面,并将vm.agreemnt传入到confirm.dialog.html中 main.html 在此页面中使用cost-info来传输数据,将main.controller中的vm.agreement传递给cost-info ,o

angular编译机制

转载https://segmentfault.com/a/1190000011562077 Angular编译机制 前言 这是我用来进行实验的代码,它是基于quickstart项目,并根据aot文档修改得到的.各位可以用它来进行探索,也可以自己基于quickstart进行修改(个人建议后者). 2018年2月17日更新:最近又做了2个小Demo用来研究Angular的编译和打包,基于Angular5,一个使用rollup,一个使用webpack,(rollup目前无法做到Angular的lazy

angularjs学习笔记--主html&amp;template html&amp;module&amp;template js、模块、控制器、双向数据绑定、过滤器

// Register the `phoneList` component on the `phoneList` module, angular. module('phoneList'). component('phoneList', {...}); // Define the `phonecatApp` module angular.module('phonecatApp', [ // ...which depends on the `phoneList` module 'phoneList'

[Angular Directive] Combine HostBinding with Services in Angular 2 Directives

You can change behaviors of element and @Component properties based on services using @HostBinding in @Directives. This allows you to build @Directives which rely on services to change behavior without the @Component ever needing to know that the Ser

[Angular 2] 9. Replace ng-modle with #ref &amp; events

Let's say you want to write a simple data bing app. when you type in a text box, somewhere in the application will show the result. In Angular 1, you can use ng-model to finish all those stuff,  but in angular 2, the concept behind has changed. <!--

[Angular Tutorial] 13 -REST and Custom Services

在这一步中,我们将会改变我们获取数据的方式. ·我们定义一个代表RESTful客户端的自定义服务.使用这个客户端,我们可以用一种更简单的方法向服务端请求数据,而不用处理更底层的$httpAPI,HTTP方法和URLs. 最重要的变化列举如下,您可以点击这里在GitHub上查看全部的不同. 依赖 RESTful功能由Angular的ngResource模块提供,这是从Angular核心模块中独立出来的. 由于我们使用了Bower来安装客户端的依赖,这一步更新bower.json配置文件来添加新的依