首页 > 在原生js中如何可以调用AngularJS的内部方法?

在原生js中如何可以调用AngularJS的内部方法?

遇到一个问题,就是在其他的原生js里,需要调用到angularjs controller里的方法。

原本想到的是在页面上id为test的html标签写个ng-click,然后在代码里用test.click();来触发点击事件,从而调用到angularjs controller里的方法。

但是这个方式在chrome或者其他浏览器里好使,在ie里就悲剧了。。一直报Error: [$rootScope:inprog] http://errors.angularjs.org/1.2.20/$rootScope/inprog?p0=%24apply。。。

请问还有其他更好的解决方法么?


<div id="app" ng-app="app" ng-controller="ctrl">
    <a ng-if="show" onclick="func1()">click</a>
</div>
<script src="http://cdn.bootcss.com/angular.js/1.5.6/angular.min.js"></script>
<script>
    angular.module('app', []).controller('ctrl', function ($scope, $timeout) {

        $timeout(function () {
            $scope.show = true
        })

        $scope.func1 = function () {
            alert(arguments[0])
        }
    })

    function func1() {
        angular.element(document.getElementById('app')).scope().func1('123')
    }
</script>
【热门文章】
【热门文章】