3
votes

In Angular 1 we set ng-controller for any HTML tag in the Razor view and use the functions defined in the angular controller. How I can achieve this view Angular2? For example I want to click on a HTML tag in Razor View and call a method from an angular2 component or directive.

Any help will be appreciated.

2
The controller is basically the component constructor now. The controller methods are now the component methods.pixelbits

2 Answers

0
votes

There is no more concept of controller in Angular. Views are now managed by components.

Within template associated with a component, you can leverage its methods and its state. Here is a sample below:

import {Component} from 'angular2/core';
import {CompanyService, Company} from './app.service';

@Component({
    selector: 'company-list',
    template: `
      <h1>Companies</h1>
      <ul>
        <li *ngFor="#company of companies">
          <a href="#" (click)="selectCompany(company)">
            {{company.name}}
          </a>
        </li>
      </ul>
    `
})
export class ListComponent {
  constructor(service:CompanyService,router:Router) {
    this.service = service;
    this.companies = service.getCompanies();
  }

  selectCompany(company:Company) {
    (...)
    return false;
  }
}

The click event is attached to the selectCompany method using the syntax (click).

Hope it helps you, Thierry

0
votes

What is I searching for ?

  • A framework with Angularjs functionalities but simpler like Angular2.

Angular2 unfortunately start the app whit one component and what I want (multi components in on page) not possible.

I would like to experiment Vue (a framework that bring angular 1 and 2 together).