Using angularJS function in phantomJS

不想你离开。 提交于 2020-01-25 07:40:08

问题


I searched on the net and I found nothing. I have a script angularJS which appear or disappear element from my html page. I use a phantomJS script to access to the html page and do some stuffs. But Can I use the $scope or the functions of my angular script in my phantom script ? I know that I can use jQuery but angular i don't know.


回答1:


I finally found. A script PhantomJS use javascript so we just have to call angularJS element like in javascript:

This is angular.element($("#someThing")).scope(); to get the scope.

So we can have this example of phantomJS :

page.open('http://www.google.com', function (status) {
    if ('success' !== status) {
        console.log("Error");
    } else {
        page.evaluate(function() {          
            angular.element($("#someThing")).scope().aFunctionInTheScope();
            var scope = angular.element($("#myDiv")).scope();
            var width = scope.getWidth();
        });
        phantom.exit();
    }
});

with an angularJS code like this :

$scope.getWidth = function() {
     return 1600;
}

and an html page like this :

<div id="myDiv">
    blabla
</div>


来源:https://stackoverflow.com/questions/30502454/using-angularjs-function-in-phantomjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!