Martijn Bos
Martijn Bos

Reputation: 125

AngularJS If else statement and/or switch

I've been tinkering around with a bit of code in AngularJS. I ran into a slight problem. I would like the user to select from 3 options... (Look below for my HTML for )

<label class="item item-input item-select">
    <div class="input-label">
        Frequentie
    </div>
    <select name="multipleSelect" id="multipleSelect" ng-model="data.multipleSelect">
        <option value="eenmalig" >Eenmalig</option>
        <option value="maandelijks">Maandelijks</option>
        <option value="wekelijks">Wekelijks</option>
    </select>
</label>

On request the code for the activating button:

<div class="bar ">
    <input type="button"
           ng-click=addToNgRepeatMinus("uitgiften");startAgain()
           value="uitgaven"
           class="button button-dark"
           id="uitgiftenbutton">

    <input type="button"
           ng-click=addToNgRepeatPlus("inkomsten");startAgain()
           value="inkomsten"
           class="button button-dark"
           id="inkomstenbutton">
</div>   

Now my general understanding was that I could let certain things happen depending on which statement they would choose to select. (Which I am sure you can, but I just can't atm!)

first, I tried to use if, if else and else for it.. but I discovered that the if else statement isn't available in AngularJS.(Please correct me if it is!)

So naturally, I tried to use a switch.. which doesn't work sadly!

$scope.addToNgRepeatMinus = function(data) {
    if(data === "uitgiften"){
        switch(multipleSelect) {
            case "eenmalig":
                $scope.uitgiften.push({name: $scope.formulier.name,value: $scope.formulier.value});
                break;
            case "maandelijks":
                $scope.uitgiften.push({name: $scope.formulier.name,value: $scope.formulier.value});
                break;
            case "wekelijks":
                $scope.uitgiften.push({name: $scope.formulier.name,value: 4*$scope.formulier.value});
                break;
        }
    }
};

This bit of code also interacts with certain buttons. (The function addToNgRepeatMinus gets activated when these buttons get pressed.) So when I press the button, It checks if the incoming data equals "uitgiften". If it does so, It should know that data.multipleSelect would equals either "eenmalig", "wekelijks" or "maandelijks" and then do the correct thing.

Can someone help me with this? What am I doing wrong ? :)

Upvotes: 3

Views: 2340

Answers (2)

Navoneel Talukdar
Navoneel Talukdar

Reputation: 4588

As your dropdown(select) is mapped to data.multipleSelect ng-model so when you do select ng-model="data.multipleSelect", you are binding your select to $scope.data.multipleSelect.

So, inside your controller, if you want to have an access your selected value, you use the following : $scope.data.multipleSelect

$scope.addToNgRepeatMinus = function(data) {
  if(data === "uitgiften"){
  switch($scope.data.multipleSelect) {
    case "eenmalig":
      $scope.uitgiften.push({name: $scope.formulier.name,value: $scope.formulier.value});
    break;
    case "maandelijks":
      $scope.uitgiften.push({name: $scope.formulier.name,value: $scope.formulier.value});
    break;
    case "wekelijks":
      $scope.uitgiften.push({name: $scope.formulier.name,value: 4*$scope.formulier.value});
    break;
  }
}
};

Upvotes: 2

vincic
vincic

Reputation: 23

From the code you have shown, you should use switch($scope.data.multipleSelect)

If that doesn't work you need to provide some more code.

Upvotes: 1

Related Questions