简体   繁体   English

带有条件语句的ng-init

[英]ng-init with condition statements

I have a Angular JS Application, 我有一个Angular JS应用程序,

<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. 当页面加载时,getTableData将执行,但我想检查变量$ rootScope.Dealer并切换初始化的函数名称。

Eg : if $rootScope.Dealer value present I wanto execute the function named getDealerData 例如:如果$ rootScope.Dealer值存在,我想执行名为getDealerData的函数

And If the value is not set need to execute the getTableData function. 如果未设置该值,则需要执行getTableData函数。

How can I make this in anglar js template. 我怎么能在anglar js模板中做到这一点。

I just tried the ng-if, but its not working... 我刚尝试了ng-if,但它不起作用......

You can use simple Javascript syntax in ng-init directive like this: 您可以在ng-init指令中使用简单的Javascript语法,如下所示:

<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 这里有一个plnkr(我已将后端路由生成更改为文本): 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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