GithubHelp home page GithubHelp logo

basics-angular's Introduction

Вставка значение переменной

{{ value }}

Однонаправленный бандинг из ts в html

<img [src] = 'imgSrc'/>

Однонаправленный бандинг из html в ts

<input
    type="text"
    (input)="onInput($event)"      //$event - событие, которое можно получить
    (blur)="onBlur(myInput.value)" //получить значение инпута
    #myInput                       //типо ref 
/>

Если выражение в одну строчку, то можно сразу внутри html менять значение переменной

<button (click)="backgroundToggle = !backgroundToggle"></button>

Двунаправленный databinding, т.е. не надо создавать функцию onChange

<input [(ngModel)]="title"
       type="text"
>

Структурные директивы:

*ngIf        =    что-то типо show && <Component/>
*ngSwitch    =    switch case
*ngFor       =    для итераций

Пример if else:

<p *ngIf="toggle; else blueP" class="red">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ducimus,
  repudiandae.</p>

<ng-template #blueP>
  <p class="blue">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo, incidunt?</p>
</ng-template>

Пример switch case:

<div [ngSwitch]="toggle">
  <p *ngSwitchCase="true" class="red">Lorem ipsum.</p>
  <p *ngSwitchCase="false" class="blue">Lorem ipsum.</p>
  <p *ngSwitchDefault >Lorem ipsum.</p>
</div>

Директивы:

[(ngModel)]  =     для двунаправленного input databinding
[ngStyle]    =     для inline стилей
[ngClass]    =     для классов стилей

как передавать children.

  <app-post
    *ngFor="let p of posts"
    [post]="p"
  >
    //Нужно между открывающимся и закрывающимся тегом что-то засунуть
    <small *ngIf="p.text.length > 10; else short">Пост длинный</small>

    <ng-template #short>
      <small>
        Пост короткий
      </small>
    </ng-template>
  </app-post>


// post-component получить это через *ng-content*
  <ng-content></ng-content>

Как передать какой-то результат родителю:

  //в ребенко создать eventEmmiter
  @Output() createdPost: EventEmitter<Post> = new EventEmitter();
   
  передать значение с помощью функции emit
  this.createdPost.emit(post);

  //в родительском компоненте вызвать передать параметры с помощью $event
  <app-post-form
    (createdPost)="updatePosts($event)"
  ></app-post-form>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.