ng-init with condition statements

14,776

Solution 1

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

Solution 2

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>
Share:
14,776

Related videos on Youtube

Jishad
Author by

Jishad

Updated on September 16, 2022

Comments

  • Jishad
    Jishad over 1 year

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