Search code examples
javascriptangularjsng-switchrootscope

How to compare against global variables in ng-switch


I am using the AngularJS $rootScope object to expose some global constants that need to be accessible to both controllers and views:

var app = angular.module('myApp', []);

app.run(function ($rootScope) {
    $rootScope.myConstant = 2;
});

When I attempt to render a global value in a view, it works correctly:

{{myConstant}}

Equally, if I reference the global value in an ng-if condition it also works:

<span ng-if="someValue == myConstant">Conditional content</span>.

But, when attempting to use the same value for comparison within an ng-switch block, it never evaluates to true. This JSFiddle demonstrates my attempt to get this working. I have also tried explicitly referencing the constant value as a member of $rootScope and as an expression (inside double curly braces) but nothing works.

Any ideas what I'm doing wrong?

Thanks,

Tim


Solution

  • You could always roll your own... :)

    (Not sure how efficient this is and it's not well-tested as I just now wrote it)

    http://jsfiddle.net/H45zJ/1/

    app.directive('wljSwitch', function () {
        return {
            controller: function ($scope) {
                var _value;
                this.getValue = function () {
                    return _value;
                };
                this.setValue = function (value) {
                    _value = value;
                };
    
                var _whensCount = 0;
                this.addWhen = function (value) {
                    _whensCount++;
                }
                this.removeWhen = function (value) {
                    _whensCount--;
                }
                this.hasWhens = function () {
                    return _whensCount < -1;
                };
            },
            link: function (scope, element, attrs, controller) {
                scope.$watch(function () {
                    return scope.$eval(attrs.wljSwitchOn);
                }, function (value) {
                    controller.setValue(value);
                });
            }
        };   
    });
    
    app.directive('wljSwitchWhen', function () {
        return {
            require: '^wljSwitch',
            template: '<span ng-transclude></span>',
            transclude: true,
            replace: true,
            link: function (scope, element, attrs, controller) {
                scope.$watch(function () {
                    return controller.getValue() === scope.$eval(attrs.wljSwitchWhen);
                }, function (value) {
                    if (value) {
                        controller.addWhen();           
                    } else { 
                        controller.removeWhen();      
                    }
                    element.attr('style', value ? '' : 'display:none;');
                });
            }
        };   
    });
    
    app.directive('wljSwitchDefault', function () {
        return {
            require: '^wljSwitch',
            template: '<span ng-transclude></span>',
            transclude: true,
            replace: true,
            link: function (scope, element, attrs, controller) {
                scope.$watch(controller.hasWhens, function (value) {
                    element.attr('style', value ? '' : 'display:none;');
                });
            }
        };   
    });