简体   繁体   中英

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...

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

Try this

 var app = angular.module("myApp", []); app.controller("myCtrl", function($scope, $rootScope) { $rootScope.dealer = ["a", "b", "c"]; $scope.getTableData = function(x) { return x; } $scope.getDelearData = function() { return $rootScope.dealer; } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <div ng-init="data = getDelearData() || getTableData('table data')"> {{data}} </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM