angular2-template

how do I fire onload events for elements within component html in angular2

自作多情 提交于 2019-12-04 03:59:14
So this is a 2 in 1 question. First, I am trying to fire a function when an element, within a components html, loads. I tried it numerous ways, like: <div [onload]="myFunction()"> this however results in the function being called multiple times, 10 to be exact. My guess is this is not the way to go, but I am not familiar enough to get it working properly. Also I would like to send the element along as a parameter. For example doing <div #myDiv (click)="myFunction(myDiv)"> does work, but obviously this isn't fired onload of said element. Whats the proper way here, or am I obligated to do a

Angular 2 + Semantic UI , component encapsulation breaks style

雨燕双飞 提交于 2019-12-04 03:53:48
I'm using Angular2 with Semantic UI as a css library. I have this piece of code: <div class="ui three stakable cards"> <a class="ui card"> ... </a> <a class="ui card"> ... </a> <a class="ui card"> ... </a> </div> the cards are rendered nicely with a space between and such. like this: refer to cards section in the link since the cards represent some kind of view I thought of making a component out of it, so now the code is: <div class="ui three stakable cards"> <my-card-component></my-card-component> <my-card-component></my-card-component> <my-card-component></my-card-component> </div> but now

Angular2: what expressions can we interpolate in template

心不动则不痛 提交于 2019-12-04 03:53:43
问题 I read that we can interpolate Javascript expressions. What is the list of valid Javascript expressions that we can interpolate? So far for interpolation I have a displayed property, eg. object.property , short expressions like {{1+1}} what else Javascript expressions can we interpolate? 回答1: Expressions in Angular2 are very similar to expressions in Angular in terms of the scope of what they allow. JavaScript expressions that promote side effects are prohibited including Assignment (= +=, -=

Good template strategy for authentication in Angular 2

雨燕双飞 提交于 2019-12-04 03:39:09
I currently have an Angular 2 app up and running that looks as follows: App.component is bootstrapped when visiting the site. The template for App.component has all component tags (for example menu.component, search.component and the router-outlet). What I basically need is the following: currently a visitor is directly redirected to the Login page because the user needs to login. He is still able to see the menu and all components that are only there for logged in users. What would be the best strategy to add an extra template layer, so not logged in users get redirected? The way that I've

md-menu override default max-width in Angular 2

 ̄綄美尐妖づ 提交于 2019-12-04 03:32:14
I'm using Angular 2 , Angular Material and I am willing to display more data in a md-menu and, therefore, I need to set the max-width of the md-menu to a higher value. Its default value is of 280px. <img src="/assets/images/ic-notifications.png" [mdMenuTriggerFor]="appMenu"/> <md-menu #appMenu="mdMenu" [overlapTrigger]="false" yPosition="below" xPosition="before" class="notifications-dropdown"> <button md-menu-item > <div class="row notification-row"> <div> <span class="image-container"> Option 1 </span> </div> </div> </button> </md-menu> In CSS file, I do this: .mat-menu-panel.notifications

*ngFor is not working in angular 2

流过昼夜 提交于 2019-12-04 03:27:56
问题 app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; import { routing } from './app.routing'; import { HttpModule, JsonpModule } from '@angular/http'; import { LoginComponent } from './auth/login.component'; import { SignupComponent } from './register/signup.component'; import { HomeComponent } from './home/home.component'; import {

Accessing specific array element in an Angular2 Template

自古美人都是妖i 提交于 2019-12-04 01:26:39
I have an array that I can loop through using ng-for syntax. However, ultimately I want to access just a single element of that array. I cannot figure out how to do that. In my component script I have export class TableComponent { elements: IElement[]; } In my template, I am able to loop through the elements via <ul> <li *ngFor='let element of elements'>{{element.name}}</li> </ul> However, trying to access an item in the element array by secifically referencing an item utilizing x {{elements[0].name}}x does not seem to work. The formatting in the template is pretty explicit, so I want to be

How to trigger Angular 2 form submit from component?

依然范特西╮ 提交于 2019-12-04 01:11:40
Basically, I have <form #f="ngForm" (ngSubmit)="save(f.form)" #formElement> ... <button class="btn btn-primary" #saveButton>Save</button> </form> I want to be able to trigger submit() from the component. I've tried @viewChild('formElement') and renderer.invokeElementMethod to trigger click() . NgForm has property ngSubmit which is EventEmitter . So doing emit() on this property from the component will trigger a submit. Also you need to use your f variable instead of formElement because f is referencing to ngForm . @ViewChild('f') form: NgForm; form.ngSubmit.emit(); 来源: https://stackoverflow

Event delegation in Angular2

时间秒杀一切 提交于 2019-12-03 23:29:01
I'm developing an app in ng2 and I'm struggling with something. I'm building a calendar where you can pick a date-range and I need to react on click & mouseenter/mouseleave events on day cells. So I have a code (simplified) like this: calendar.component.html <month> <day *ngFor="let day of days" (click)="handleClick()" (mouseenter)="handleMouseEnter()" (mouseleave)="handleMouseLeave()" [innerHTML]="day"></day> </month> But this gives me hundreds of separate event listeners in the browser's memory (each day's cell gets 3 event listeners, and I can have up to 12 months displayed at a time so it

How can I pass a generic type parameter to an Angular2 component?

ε祈祈猫儿з 提交于 2019-12-03 23:24:20
Let's say I got a component with a fixed input parameter type, @Component({ selector: 'fixed', template: '<div>{{value}}</div>' }) export class FixedComponent { @Input() value: string; } How do I go about making that parameter type generic, i.e. @Component({ selector: 'generic', template: '<div>{{value}}</div>' }) export class GenericComponent<T> { @Input() value: T; } That is, how do I pass the type in the template of the parent component? <generic ...></generic> It seems that when using AOT compilation the only way to do this is to replace the generic type with 'any'. See https://github.com