JpDevs JpDevs - 1 month ago 21
AngularJS Question

ng-init with condition statements

I have a Angular JS Application,

<div class="col-lg-12" ng-init="getTableData('{{URL::route('get_repair_category')}}')">


When Page loading the getTableData will execute, but I want to check a variable $rootScope.Dealer and switch the function name of initialization.

Eg : if $rootScope.Dealer value present I wanto execute the function named getDealerData

And If the value is not set need to execute the getTableData function.

How can I make this in anglar js template.

I just tried the ng-if, but its not working...

Answer

You can use simple Javascript syntax in ng-init directive like this:

<div class="col-lg-12" ng-init="Dealer ? getDealerData('{{URL::route('get_repair_category')}}') : getTableData('{{URL::route('get_repair_category')}}')">

Here is a plnkr for you (I've changed backend route generation to text): https://plnkr.co/edit/CJOMT0g50BCWa3j02rcS?p=preview