UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
Call Angular $scope functions from console
1. How to call JavaScript, JQuery,
Angular $scope function from
Console
2. Whenwe are usingAngularJsforclientside development.We are not able tocall any$scope function
directlyfromconsole likewe cancall any functionin Javascriptand in jQuery.
But some time it’sverynecessaryto call Angular$scope functionforTesting purpose.So,TodayIam
goingto demonstrate some easytrickstocall AngularJs$scope functionfromconsole.
Followingwe have one example of simple AngularJsPage.Havingone JavaScript,One JQueryandTwo
AngularJs$scope function
Java Script Function– JsTest()
JQuery Function– JQueryTest()
AngularJs Function-
$scope.showHello()
$scope.sum()
Nowwe will call all functionfromconsole forthis open followingprogram inGoogle Chrome.Andright
clickanywhere in webpage andselect “Inspectelement”.
It will showyouscreenlike this.
5. Call JQueryfunctionfrom console
For call JQuery Functionfrom console use JQueryfunctionlike this “$().JQueryTest()”.Like this
Output:
Call AngularJs $scope function from Console
Nowcall two AngularJs$Scope functionfromconsole. Forcall AngularJs functions use following
command intoconsole:
Syntax
angular.element(document.querySelector('#someID')).scope().someFunction();
#someID – Can be anyelement’sIDwhichexistsinside ng-controller.Butit’sbettertouse idof element
where use using ng-controller.Like we are usingmyDiv’sID.
angular.element(document.querySelector('#myDiv')).scope().showHello();
It will showyou“AlertBox”havingmessage “Hello AngularJs”Like this
6. We have another$scope functioncalled“Sum”,Now we will call thisusingfollowingcommand
angular.element(document.querySelector('#myDiv')).scope().sum(100,100);
It will returnyou“200” intoconsole.Like this
You alsohave optionto assignoutputtoa variable like we assignedoutputinto Sumvariable .