Adding multiple class using ng-class

后端 未结 11 1529
不知归路
不知归路 2020-11-22 12:47

Can we have multiple expression to add multiple ng-class ?

for eg.

11条回答
  •  生来不讨喜
    2020-11-22 13:13

    Using a $scope method on the controller, you can calculate what classes to output in the view. This is especially handy if you have a complex logic for calculating class names and it will reduce the amount of logic in your view by moving it to the controller:

    app.controller('myController', function($scope) {
    
        $scope.className = function() {
    
            var className = 'initClass';
    
            if (condition1())
                className += ' class1';
    
            if (condition2())
                className += ' class2';
    
            return className;
        };
    });
    

    and in the view, simply:

提交回复
热议问题