2015-12-07 2 views
9

Come scrivere la dichiarazione dell'interruttore nel controller angularJS?Come scrivere la dichiarazione dell'interruttore nel controller angularJS

mio codice sorgente è

<!DOCTYPE html> 
<html> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<body> 

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table> 
    <tr ng-repeat="x in names"> 
    <td>{{ x.Name }}</td> 
    <td><a href="" ng-click="SwitchFuction(x.Name, x.Sno)">{{ x.Country }}</a></td> 
    </tr> 
</table> 

</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('customersCtrl', function($scope, $http) { 

    $scope.names = [ 
     { Sno: '1', Name: 'Jani', Country: 'Norway' }, 
     { Sno: '2', Name: 'Hege', Country: 'Sweden' }, 
     { Sno: '3', Name: 'Kai', Country: 'Denmark' } 
    ]; 

    $scope.SuperFunction = function (id) { 
     alert(id); 
    }; 

    $scope.SwitchFuction = function (name, sno) { 
     switch (sno) { 
      case '1' 
       alert("1. Selected Name: " + name); 
       break; 
      case '2' 
       alert("2. Selected Name: " + name); 
       break; 
      default: 

     } 
    }; 

}); 
</script> 

</body> 
</html> 

Come scrivere l'istruzione switch all'interno della funzione SwitchFuction ??? Nel codice sorgente sopra riportato contiene qualche errore semantico. Sollecitare gentilmente come scrivere la Dichiarazione di Switch?

La schermata di errore: FireFox Screen Shot

+0

Potete fornire il plunker? – Ricky

risposta

25

c'è un errore di sintassi nella tua SwitchFunction dopo ogni caso : manca codice corretto:

$scope.SwitchFuction = function (id, caseStr) { 
     switch (caseStr) { 
      case '1': 
       alert("Selected Case Number is 1"); 
       break; 
      case '2': 
       alert("Selected Case Number is 2"); 
       break; 
      default: 

     } 
    }; 
4

AngularJS è costruito sulla base di JavaScript e non ha alcuna sintassi diversa per caso l'interruttore di JavaScript (Fino a quando lo si utilizza nello script). Istruzioni di switch switch di supporto JavaScript con la seguente sintassi.

switch (expression) { 
    case value1: 
    //Statements executed when the result of expression matches value1 
    [break;] 
    case value2: 
    //Statements executed when the result of expression matches value2 
    [break;] 
    ... 
    case valueN: 
    //Statements executed when the result of expression matches valueN 
    [break;] 
    default: 
    //Statements executed when none of the values match the value of the expression 
    [break;] 
} 

Reference

+0

Ok. Allora cosa c'è di sbagliato nel mio script sopra? Sostieni gentilmente il mio script per rendere privo di bug ... –

+0

Qual è l'errore che ottieni? – Vivek

+0

Ho allegato la schermata. Si prega di riferirlo. –

3

<!DOCTYPE html> 
 
<html> 
 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<body> 
 

 
    <div ng-app="myApp" ng-controller="customersCtrl"> 
 

 
    <table> 
 
     <tr ng-repeat="x in names"> 
 
     <td>{{ x.Name }}</td> 
 
     <td><a href="" ng-click=SwitchFuction(x.Name,$index)>{{ x.Country }}</a> 
 
     </td> 
 
     </tr> 
 
    </table> 
 

 
    </div> 
 

 
    <script> 
 
    var app = angular.module('myApp', []); 
 
    app.controller('customersCtrl', function($scope) { 
 

 
     $scope.names = [{ 
 
     Name: 'Jani', 
 
     Country: 'Norway' 
 
     }, { 
 
     Name: 'Hege', 
 
     Country: 'Sweden' 
 
     }, { 
 
     Name: 'Kai', 
 
     Country: 'Denmark' 
 
     }]; 
 

 
     $scope.SuperFunction = function(id) { 
 
     alert(id); 
 
     }; 
 

 
     $scope.SwitchFuction = function(id, caseStr) { 
 
     switch (caseStr) { 
 
      case 0: 
 
      alert("Selected Case Number is 0"); 
 
      break; 
 
      case 1: 
 
      alert("Selected Case Number is 1"); 
 
      break; 
 
      default: 
 
      alert("Selected Case Number is other than 0 and 1"); 
 
      break; 
 

 
     } 
 
     }; 
 

 
    }); 
 
    </script> 
 

 
</body> 
 

 
</html>

Ecco lo Plunker

+0

Spiega anche il tuo codice. – KittMedia

+0

sopra è il codice corretto per la sintassi della domanda posta. Qui vengono discussi il passaggio dello script java e il passaggio degli argomenti. leggi di più su $ index a: [qui] (https://thinkster.io/egghead/index-event-log) – DeviD