需求:安卓端调用原生js方法,但是前端框架是用angularJs写的,安卓端是直接调用是不起作用的。所以需要原生js调用angularjs方法

html

<div class="mainBody" ng-controller = 'lostAndFoundCtrl'>
    `<input type="text" placeholder="请输入物品名称" ng-model="keyWord" />`
</div>

html中script标签代码

//安卓端调用,此方法给安卓端共用
    function pickupMethod(msg){
        console.log(msg);
         var appElement = document.querySelector('[ng-controller=myCtrl]');  //对应前端ng-controller=myCtrl
          var $scope = angular.element(appElement).scope(); 
         $scope.pickupMethod(msg); //调用controller方法
    }

写在controller的js中

    $scope.pickupMethod = function(msg){
        console.log(msg);
        $scope.keyWord = msg;//赋值
    }

这样绝对可以的~


diuren1205
57 声望5 粉丝

I love coding