Using angularJS function in phantomJS

2019-09-14 16:58发布

问题:

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>