2015-12-07 17 views
9

Làm thế nào để viết báo cáo chuyển đổi trong bộ điều khiển angularJS?Cách viết Lệnh chuyển đổi trong bộ điều khiển angularJS

My Source Code là

<!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> 

Làm thế nào để Viết câu lệnh switch bên trong hàm SwitchFuction ??? Trong Mã nguồn ở trên chứa một số lỗi ngữ nghĩa. Vui lòng hỗ trợ cách viết Tuyên bố chuyển đổi?

Các Screen Lỗi: FireFox Screen Shot

+0

Bạn có thể cung cấp plunker không? – Ricky

Trả lời

25

có một lỗi cú pháp trong SwitchFunction của bạn sau mỗi trường hợp : thiếu mã đúng:

$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 được xây dựng trên đầu trang của JavaScript và nó không có cú pháp khác nhau đối với trường hợp chuyển đổi hơn JavaScript (Chừng nào bạn đang sử dụng nó trong kịch bản). Các câu lệnh trường hợp chuyển đổi hỗ trợ JavaScript với cú pháp sau.

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. Sau đó, những gì sai trong kịch bản trên của tôi? Vui lòng hỗ trợ Tập lệnh của tôi để phát hiện lỗi ... –

+0

Lỗi bạn đang gặp phải là gì? – Vivek

+0

Tôi đã đính kèm Ảnh chụp màn hình. Vui lòng tham khảo nó. –

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>

Đây là số Plunker

+0

Vui lòng giải thích mã của bạn. – KittMedia

+0

ở trên là mã được sửa lỗi cú pháp của câu hỏi được hỏi. Ở đây, chuyển đổi tập lệnh java và đối số đi qua được thảo luận. đọc thêm về $ index tại: [here] (https://thinkster.io/egghead/index-event-log) – DeviD

Các vấn đề liên quan