Example of an interactive function between a ANGULARJS command and a controller _ANGULARJS

Source: Internet
Author: User
Tags bind

An example of this article describes the interaction between the ANGULARJS command and the controller. Share to everyone for your reference, specific as follows:

In this section, we look at the interaction between the controller and the instruction

1. First of all, the simplest way to call the parent controller in an instruction:

<div ng-controller= "MyController1" >
</div>

App.controller (' MyController1 ', [' $scope ', function ($scope) {
    $scope. Load=function () {
     console.log (' Loading data ......');
    }
}]);
App.directive (' MyTest ', function () {
    return{
      restrict: ' E ',
      link:function (scope,ele,attr) {
        Ele.bind (' MouseEnter ', function () {
          scope.load ();}}}}
)

In the first section, we already know how to invoke the method in the parent controller in the scope parameter of the instruction, here:

Scope.load ();

Can also be written as:

Scope. $apply (' Load () ');

2. The same instruction, in the dynamic invocation of multiple parent class controller methods:

<div ng-controller= "MyController1" loadmore= "Load1 ()" </div>
<div ng-controller= "MyController2" Loadmore= "Load2 ()" ></div>

App.controller (' MyController1 ', [' $scope ', function ($scope) {
    $scope. Load1=function () {
     Console.log (' Loading data ... '}
}]);
App.controller (' MyController2 ', [' $scope ', function ($scope) {
    $scope. Load2=function () {
    Console.log (' Loading data ... ";
    }
}]);;
App.directive (' MyTest ', function () {
    return{
      restrict: ' E ',
      link:function (scope,ele,attr) {
        Ele.bind (' MouseEnter ', function () {
          scope. $apply (Attr.loadmore ())}}}}
)

Note here:

Scope. $apply (Attr.loadmore ());

Loadmore is lowercase, and in HTML, attributes are obtained through the rules of the hump.

More readers interested in ANGULARJS related content can view the site topics: "Angularjs Introduction and Advanced Tutorials" and "ANGULARJS MVC Framework Summary"

I hope this article will help you to Angularjs program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.