Use JS outside the angular to invoke the function method or variable __ data structure provided by angular controller

Source: Internet
Author: User

The HTML code looks like this:

1 <! DOCTYPE html>
 2 

The JavaScript code looks like this:

1     var ngapp = angular.module (' myApp ', []);
 2     ngapp.controller (' Mycontroller ', function ($scope, $http) {
 3         $scope. msg = ' Hello, angular. ';
 4         $scope. GetData = function () {
 5 return             ' qubernet ';
 6         }
 7     });
 8 
 9     onload = function () {         document.getElementById (' lbtntest '). onclick = function () {
11< c19/>//through controller to get angular application             var appelement = Document.queryselector (' [Ng-controller=mycontroller ]');             get $scope variable             var $scope = angular.element (appelement). scope ();             Call the MSG variable and change the value of MSG             $scope. msg = ' 123456 ';
The             value of MSG is changed on the///last line, and if you want to sync to the angular controller, you need to call the $apply () method to             $scope. $apply ();             the GetData () method             Console.log ($scope. GetData ())
in the//Call controller. }     

The effect is shown in the following image before clicking the "Call" button:

After clicking on the "Call" button, the effect is shown in the following image:

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.