Claudio Pomo
Claudio Pomo

Reputation: 2472

angular ng-class change without click

I've a trouble with ng-class. I want to change the boolean value to control ng-class but this value is setted in async mode and view file is just loaded.

this is .html view

<li ng-class="{active: isPageView('media')}">
                <div class="pointer" ng-show="isPageView('media')">
                    <div class="arrow"></div>
                    <div class="arrow_border"></div>
                </div>
                <a ng-class="{true: 'dropdown-toggle', false: ''}[disabled]" href="">
                    <i class="icon-camera-retro"></i>
                    <span>Media</span>
                    <i class="icon-chevron-down"></i>
                </a>
                <ul class="submenu">
                    ...........
                </ul>
            </li>

and this is part of controller

$scope.init = function(){
        _.mixin(_.str.exports());
        $scope.disabled = true;
        $rootScope.emptyCompose();
        var localstorage = localStorageService.get('Userprofiles');
        var allExpired = true;
        if (localstorage) {
            $rootScope.userprofiles = localstorage;
            _.each($rootScope.userprofiles, function(userprofile){
                if(moment().isBefore(moment.unix(userprofile.expireTime))){
                    allExpired = false;
                }
            });
            if(allExpired){
                $scope.disabled = ! $scope.disabled;
                $rootScope.modalInstance = $modal.open({
                    templateUrl: 'views/composebilling.html',
                    controller: 'BillingCtrl',
                    keyboard: false,
                    backdrop: 'static'
                });
            }
        }else {
            Index.query(function(userprofiles) {
                $rootScope.userprofiles = userprofiles;
                localStorageService.add('Userprofiles', userprofiles);
                _.each($rootScope.userprofiles, function(userprofile){
                    if(moment().isBefore(moment.unix(userprofile.expireTime))){
                        allExpired = false;
                    }
                });
                if(allExpired){
                    $scope.disabled = ! $scope.disabled;
                    $rootScope.modalInstance = $modal.open({
                        templateUrl: 'views/composebilling.html',
                        controller: 'BillingCtrl',
                        keyboard: false,
                        backdrop: 'static'
                    });
                }
            });
        }

If $scope.disabled is setted without query on server all fire well, otherwise I've trouble

Upvotes: 0

Views: 359

Answers (1)

MBielski
MBielski

Reputation: 6620

You are using ng-class with isPageView('media') yet that function is not in the code sample you provided. If anything is dependant upon $scope.disabled I don't see it. Also, this ng-class statement is written incorrectly:

<a ng-class="{true: 'dropdown-toggle', false: ''}[disabled]" href="">

It should be written something like this:

<a ng-class="{'dropdown-toggle': true-condition, 'disabled': false-condition}" href="">

... where true-condition is some expression that will evaluate to TRUE and false-condition is some expression that will evaluate to FALSE. Always setting those to TRUE and FALSE is useless because you can do that without ng-class. Having a CSS class named true or false is just bad code.

Upvotes: 1

Related Questions