Use JS to call a function method or variable provided in the angular controller outside of angular

Source: Internet
Author: User

The HTML code looks like this:

1 <!DOCTYPE HTML>2 <HTMLNg-app= "MYAPP"ID= "MYAPP">3 <Head>4     <Metaname= "Viewport"content= "Width=device-width" />5     <title>Test</title>6     <Scriptsrc= "~/content/js/plugins/angularjs/angular.min.js"></Script>7 </Head>8 <BodyNg-controller= "Mycontroller">9 {{msg}}Ten     <ahref= "javascript:;"ID= "Lbtntest">Call</a> One </Body> A </HTML>

The JavaScript code looks like this:

1     varNgapp = Angular.module (' myApp '), []);2Ngapp.controller (' Mycontroller ',function($scope, $http) {3$scope. msg = ' Hello, angular! ‘;4$scope. GetData =function () {5             return' Qubernet ';6         }7     });8 9onload =function () {Tendocument.getElementById (' Lbtntest '). onclick =function () { One             //get angular applications with a controller A             varAppelement = Document.queryselector (' [Ng-controller=mycontroller] '); -             //Get $scope variable -             var$scope =angular.element (appelement). scope (); the              -             //Call the MSG variable and change the value of the MSG -$scope. msg = ' 123456 '; -             //The previous line changes the value of MSG, and if you want to synchronize to the angular controller, you need to call the $apply () method to + $scope. $apply (); -             //Call the GetData () method in the controller + Console.log ($scope. GetData ()); A          } at}

Before clicking on the "Invoke" button, the effect looks like this:

After clicking on the "Invoke" button, the effect looks like this:

Use JS to call a function method or variable provided in the angular controller outside of angular

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.