0

I am learning Angular now. And i am just stuck in a Data binding concept. I have wrote below code in 2 file. But it won't work.

app.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  constructor(){}
  name = 'Rakesh';
  ngOnInit(){
  } 
  changeMyTitle() {
    this.name = 'Rocky';
  }
}

app.component.html

 <p>My name is {{ name }} </p>
" Here is a Button code in Html calling changeMyTitle()"
1
  • 4
    Are you just looking for <button (click)="changeMyTitle()">Change Name</button>?
    – Woohoojin
    Commented Oct 25, 2019 at 14:51

1 Answer 1

1

You need to bind click event to your button and invoke changeMyTitle() defined in your component.

Just use this in your HTML:

<p>My name is {{ name }} </p>
<button (click)="changeMyTitle()">Change name</button>

here is the stackblitz with working example.

2
  • yes, i have used that but it still won't display the name. First of all i just want to display on paragraph. What should i do to display name? I have searched that in many area. But no luck. So finally i wrote here.
    – Rakesh
    Commented Oct 26, 2019 at 13:02
  • See the stackblitz for the working example : stackblitz.com/edit/angular-dmpgyu
    – nircraft
    Commented Oct 28, 2019 at 12:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.