Angularjs How do you call a controller method inside a javascript function defined in the html template file?

前端 未结 2 1750
清酒与你
清酒与你 2021-01-16 09:16

I read this link: AngularJS access scope from outside js function and I tried to get access to the controller but I didn\'t setup my index.html like the fiddlejs example.

相关标签:
2条回答
  • 2021-01-16 10:03

    I figured it out with help from a friend. Here is the solution:

    in the signin.html file add an id to the div at the top. In the script code use that id to get to the scope using jquery (you don't need to use the angular.element()).

    signin.html

    <div id="loginwidget">
       <div role="main" id="content" >
           <div id="signIn" >
           </div>
       <div>
     <div>
    
     <script>
        $.ready('loginwidget_main', function () { 
          loginwidget.load('signin', function () {
               done : success,
               fail : fail  
          });
      });
    
     function success() {
            var scope = $('#loginwidget').scope();
            scope.test();
     }
    
     function fail() {alert("failed to login");}
    
     </script>
    
    0 讨论(0)
  • 2021-01-16 10:18
    You can check below code snippet here we are calling the loginwidget scope function in javascript function.
    
    <div id="parentController" ng-controller="parentController">
        <div id='myapp' ng-view ng-cloak></div>
    </div>
    
    function myFunction(){
          angular.element(document.getElementById('parentController')).scope().myFunction();
    }
    

    Note: Here you need to define your function in parentController.

    0 讨论(0)
提交回复
热议问题