0
$scope.$on('msg', function(e, msg)
{
    toastr.error("Error. Err-01", "Invalid Login!");

});

This is event handler function in angular 1.How can I convert in to angular 2?

1
  • What event is msg? There are different ways in Angular2 but there is no direct translation from your code. There is no $scope anymore. What problem do you actually try to solve. Can you please elaborate a bit more? Commented Jan 12, 2016 at 8:24

1 Answer 1

2

Events are now defined on components themselves. There is no more concept of scope in Angular2...

You can attach handlers on them from the HTML elements:

<my-component (someEvent)="handleEvent($event.value)"></my-component>

or within JavaScript code:

this.myEventEmitter.subscribe(
  ...
)

Defining the myEventEmitter can be done within a component that way:

@Component({
  (...)
})
export class DropdownComponent {
  @Output()
  myEventEmitter: EventEmitter;

  constructor(private elementRef:ElementRef) {
    this.myEventEmitter = new EventEmitter();
  }

  select(value) {
    this.myEventEmitter.emit(value);
  }
}

Hope it helps you, Thierry

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

1 Comment

EvenEmitter is just fine and seems to be replacing the $on('event') part. But how to use toastr's different methods in TS as they are written in Javascript? I guess toastr.success(),toastr.error() would just not work as expected.

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.