Angular variable in component not rendering in template - node.js

Answer: I was using title: 'myTitle' instead of title = 'myTitle' ;(
I have just generated a new Angular app with one new component.
The problem is when i initialize a variable inside the class component and try to output it in the template using {{}} it is not showing variable's value.
In the main - App-Root Component it is written just like my code but there it is working :(
content.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'app-content',
templateUrl: './content.component.html',
styleUrls: ['./content.component.sass']
})
export class ContentComponent {
title: 'Content'
}
content.component.html
<h3>{{title}}</h3>

This is how you should bind values :
In component.ts :
public title:any = 'Content';
in component.html :
<h1> {{title}} </h1>
Here is a working example : demo

Use the angular variable as below
title: string="Content"
For eg
Try this, I have created a sample MyComponent class as below.
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
private myVariable:string="Hello World"
constructor() { }
ngOnInit() {
}
}
my-component.component.html
<p>
{{myVariable}}
</p>
Make sure add the above component in app.component.html which is the bootstrapping component as below
<app-my-component></app-my-component>
also in app.module as below in declartion section
declarations: [
AppComponent,
TopBarComponent,
ProductListComponent,
MyComponentComponent
],
bootstrap: [ AppComponent ]

Related

Angular 7 ngx-translate change run time transaltion without refresh page

Notes: I tired all questions & answers related to this topic.
I want to Translate Typescript variable Value without refresh page on change language Dropdown .
I trying To change the language-wise data change. I success to change to HTML Bind: value on dropdown value change but not update TypeScript Bind: value.
i use ngx-translate
I referer Links: but not success
angular-ngx-translate-usage-in-typescript
ngx-translate-in-ts-file-angular
ngx-translate-with-dynamic-text-on-ts-file
components.ts
import { Component, OnInit } from '#angular/core';
import { TranslateService } from '#ngx-translate/core';
#Component({
selector: 'app-translate',
templateUrl: './translate.component.html',
styleUrls: ['./translate.component.css']
})
export class TranslateComponent implements OnInit {
typeScriptvalue: string;
simpleProducts:any[]=[ {
name:'English',
id:'en'
},
{
name:'French',
id:'fr'
}];
constructor(private translate: TranslateService) {
this.typeScriptvalue = this.translate.instant('HOME.TITLE');
}
ngOnInit(): void {
}
changeevent(e)
{
console.log(e);
this.translate.use(e.value);
}
}
component.html
<label><b> HTML Bind</b></label> : <div>{{ 'HOME.TITLE' | translate }}</div> <br>
<label><b>TypeScript Bind</b></label> : <div>{{ typeScriptvalue }}</div> <br>
<label>
Change language : <div class="dx-field-value">
<dx-select-box [dataSource]="simpleProducts" displayExpr="name" valueExpr="id" (onValueChanged)="changeevent($event)"></dx-select-box>
</div>
</label>
After long research I have finally got the best solution.
You can reassign the Typescript variable on the subscriber method. use method
changeevent(e)
{
console.log(e);
this.translate.use(e.value).subscribe(data => {
this.typeScriptvalue = this.translate.instant('Home.Title');
});
}

Cannot register svg icon

I would like to register a new Material2 icon, I have this code:
import {Component, OnInit} from '#angular/core';
import {MatIconRegistry} from '#angular/material/icon';
import {DomSanitizer} from '#angular/platform-browser';
#Component({
selector: 'app-control-panel',
templateUrl: './control-panel.component.html',
styleUrls: ['./control-panel.component.scss']
})
export class ControlPanelComponent implements OnInit {
constructor(iconRegistry: MatIconRegistry, sanitizer: DomSanitizer) {
console.log('registering play button icon'); // << confirm
iconRegistry.addSvgIcon('play_button',
sanitizer.bypassSecurityTrustResourceUrl('assets/images/play-button.svg'));
}
ngOnInit() {
}
}
the log statement above is being hit. And I have this HTML
<button mat-raised-button color="accent">
<mat-icon>play_button</mat-icon>
Start Recording
</button>
if I use an existing icon it works, but if I use "play_button" it doesn't work.
Anyone know what might be going wrong?
<mat-icon> should set the svgIcon property. i.e.:
<mat-icon svgIcon="play_button"></mat-icon>
MatIconRegistry should be imported from #angular/material
Ensure that you import HttpClientModule in app.module.ts so the asset you're requesting can be downloaded.

Using Node module with out type defination in Angular 2

I have written a very simple node module [ npm install twinconsole ] and published to npm.
I also included browser build ( umd module ) so that it can be
used from browser as well.
Below is node module related code
module.exports.print = msg => {
console.log(msg);
}
Now I would like to use this node module from my Angular 2 typescript application, to do that
I have included below CDN file in index.html.
< script src="https://npmcdn.com/twinconsole#1.2.1/dist/index.umd.min.js">
What is that I need to do so that print() function exported in node module can be used in below root component
import { Component } from '#angular/core';
declare var twinconsole: any; // ADDEED THIS LINE , IS THIS CORRECT ?
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}
import { Component } from '#angular/core';
//define interface to get "intellisense" aka autocomplete and type errors
interface TwinConsole{
print(msg:string);
}
declare var twinconsole: TwinConsole;
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}

Parameter in Ionic2 custom component not displayed

I am trying to build a simple component, just a div that prints a parameter but the parameter isn't been displayed:
test.html
<ion-content padding class="getting-started">
<my-component [test]="Something"></my-component>
</ion-content>
test.ts
import {Page} from 'ionic-framework/ionic';
import {MyComponent} from './myComponent';
#Page({
templateUrl: 'build/pages/test/test.html',
directives: [MyComponent]
})
export class TestPage {
constructor() {
}
}
myComponent.ts
import {Component,Input} from 'angular2/core';
#Component({
selector: 'my-component',
template: `
<div>Param: {{test}}</div>
`,
})
export class MyComponent {
#Input() test;
constructor() {
}
}
The result:
I can't figure it out what I am missing.
Your code is the same as the documentation (https://angular.io/docs/ts/latest/api/core/Input-var.html) except for the [test]="Something", have your tried to write test="Something" instead ?
I think [] syntax can take only variable of component.
I'm not sure, but try [test]="'Something'"

Integrating WinJS and Angular2

I'm trying to wrap a WinJS rating control in an Angular2 component. When I debug, I can see that WinJS.UI.processAll(); is being called and executed. But I don't see the rating control.
How can I make it work?
Dashboard.cshtml:
#{
ViewBag.Title = "Dashboard";
}
<my-app></my-app>
<script src="/jspm_packages/system.js"></script>
<script src="/config.js"></script>
<script>
System.import('reflect-metadata')
.then(function () {
System.import('angular2')
.then(function () {
System.import("/js/app");
});
});
</script>
app.js:
import { Component, View, bootstrap } from 'angular2';
import 'reflect-metadata';
import 'winjs';
#Component({
selector: 'my-app'
})
#View({
template: '<div data-win-control=\'WinJS.UI.Rating\' data-win-options=\'{averageRating: 3.4}\'></div>'
})
class MyAppComponent {
constructor() {
}
onInit() {
WinJS.UI.processAll();
}
}
bootstrap(MyAppComponent);
As requested by #wonderfulworld
First of all, according to Adding the Windows Library for JavaScript to your page you must add the css file to your html.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/winjs/4.3.0/css/ui-light.min.css">
Second thing, from alpha37+ you must import and implement the lifecycle hook you're using, onInit in this case (see remove LifecycleEvent).
import { Component, View, bootstrap, OnInit} from 'angular2/angular2';
import 'reflect-metadata';
import 'winjs';
#Component({
selector: 'my-app'
})
#View({
template: '<div data-win-control=\'WinJS.UI.Rating\' data-win-options=\'{averageRating: 3.4}\'></div>'
})
class MyAppComponent implements OnInit {
onInit() {
WinJS.UI.processAll();
}
}
And that would be all. Here's the plnkr.
Glad it helped ;)

Resources