How can we write this in <div ng-if= " ". the like expression and everything else

36 views Asked by At
(( (table1.field1 like 'HR%') OR (table1.field1 like 'ABC%' OR table1.field1 like 'XYZ%') )) 
1

There are 1 answers

0
Esaith On

table1.field1 like 'HR%' OR table1.field1 like 'ABC%' OR table1.field1 like 'XYZ%'

Instead of having all that logic in the html if the table1.field1 isnt going to change all that often then its best to set the boolean value once in a method and then ng-if that boolean value. This way your html does not have to do all that work on every digest cycle. Secondly, you can step through your code a lot easier in the controller rather than the HTML. Also makes the HTML cleaner.

// in controller
function ControllerFn($scope) {

    $scope.vm = {
        show: false;
    }


    function toShow(field) {
        if (!field)
            return false;
            
        return field.indexOf('HR') > -1 || field.indexOf('ABC') > -1 || field.indexOf('XYZ') > -1;
    }
    
    if (table1) {
        $scope.vm.show = toShow(table1.field1);
    }
}


// html
<div ng-if='vm.show'></div>