1

I have in my html angularjs select with null posibility:

<span class="nullable"> 
    <select ng-model="ViewLevel" ng-change="selChanged()"
            ng-options="camptype.name for camptype in campTypes">
                <option value="">-- choose campaign type --</option>
    </select>
</span>

and in js controller method to check if user selected something from list or nullable option

     $scope.selChanged = function() {
            if ($scope.ViewLevel == null) {
                $scope.getTypeData();
                console.log("nullek");
            } else {
                $scope.getCampaignData();
                console.log("nie nullek");
            }
        }

But it doesn't work. Always if clause is true, even if in firebug I can see that ViewLevel is not null. Why?

EDIT: screen from firebug
ViewLevel is an object with property but if clause was true:

diagram

3
  • What about if (!$scope.ViewLevel) {? Commented Aug 5, 2014 at 14:43
  • seems to work fine jsbin.com/jekuv/1/edit?console,output Commented Aug 5, 2014 at 14:46
  • @sylwester doesnt work in my project. Outputing this variable in gui works fine but if clause is still always true Commented Aug 6, 2014 at 8:34

1 Answer 1

1

rather than checking against null, using ! operator, it will check for both null/undefined

    $scope.selChanged = function() {
        if (!$scope.ViewLevel) {
            $scope.getTypeData();
            console.log("nullek");
        } else {
            $scope.getCampaignData();
            console.log("nie nullek");
        }
    }

EDIT

as per angularjs recommendation: your ngModel must have a dot, and that would solve the problem for you.

//somewhere in your controller
$scope.selected = {}

and then in your html

<select ng-model="selected.ViewLevel" ng-change="selChanged()"
        ng-options="camptype.name for camptype in campTypes">
            <option value="">-- choose campaign type --</option>
</select>

and then again fix if in your function. check for $scope.selected.ViewLevel

 $scope.selChanged = function() {
    if (!$scope.selected.ViewLevel) {
        $scope.getTypeData();
        console.log("nullek");
    } else {
        $scope.getCampaignData();
        console.log("nie nullek");
    }
   }
Sign up to request clarification or add additional context in comments.

6 Comments

Doesn't changed anything, still if clause is always true.
Can you setup a plunker
after copying code to plunker it works... but in my project it doesnt
in that case.. something in your setup is having the problem, i suspect that $scope.ViewLevel is not getting updated... can you console log $scope.ViewLevel and check
In batarank I found that ViewLevel is in scope which is child of my scope. How to access it ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.