3

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.

1
  • The controller is basically the component constructor now. The controller methods are now the component methods. Commented Dec 30, 2015 at 8:58

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for elaborating Thierry. My issue is adding behavior to existing HTML tags. I don't want to having template.
I create a plunker to clarify what is in mind.Please look at it here.plnkr.co/edit/TpsBwe?p=preview
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.