2015-10-15 281 views
0

我在html5和angular js中編寫代碼。在傳遞參數時遇到空間問題。這裏是我的代碼:將參數傳遞給函數空間

angular.module('MyApp', []) 
 
    controller('MyCtrl', function($scope) { 
 
     
 
     $scope.validate = function(description,code) { 
 
     alert(description,code); 
 
     } 
 
     });
<!DOCTYPE html> 
 
<html> 
 
<body ng-app="MyApp"> 
 
    <div ng-repeat="x in allItem.items" class="col-75" ng-controller="MyCtrl"> 
 
    <a href="#" ng-click="validate({{x.itemDesc}},{{x.itemCode}})"> 
 
    <div class="card item center">{{x.itemName}} 
 
    </div></a> 
 
    </div> 
 
    </body> 
 
    </html>

的JSON格式是這樣 的Json包含

allItem 
    { 
    items 
     { 
      itemName: "egg", 
      itemDesc: "Egg is very delicious", 
      itemCode: "EGG" 
      } 

     { 
      itemName: "Tomato", 
      itemDescr: "Tomato's skin contains vitamins", 
      itemCode: "TMT" 
      } 
    } 

我不能傳遞的參數。我不知道如何傳遞包含空格和引號(')的文本。任何人都可以幫我

回答

1

你的Json需要是一個數組,你可以跳過href =「#」,你不需要在ng-click內綁定。下面的代碼完全正常運行。

angular.module('MyApp', []) 
 
    .controller('MyCtrl', function($scope) { 
 

 

 
     $scope.allItem = 
 
     { 
 
      items:[ 
 
       { 
 
        itemName: "egg", 
 
        itemDesc: "Egg is very delicious", 
 
        itemCode: "EGG" 
 
       }, 
 

 
       { 
 
        itemName: "Tomato", 
 
        itemDesc: "Tomato's skin contains vitamins", 
 
        itemCode: "TMT" 
 
       } 
 
] 
 
     }; 
 

 

 
     $scope.validate = function(description, code) { 
 

 
      console.log(description,'-', code); 
 
     }; 
 
});
<!doctype html> 
 
<html> 
 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
</head> 
 
<body ng-app="MyApp" ng-controller="MyCtrl"> 
 
<br/> 
 
     
 
     <div ng-repeat="x in allItem.items" class="col-75"> 
 
      <a ng-click="validate(x.itemDesc, x.itemCode)"> 
 
       <div class="card item center">{{x.itemName}} 
 
       </div> 
 
      </a> 
 
     </div> 
 
</body> 
 
</html>

+0

謝謝@Enkode。現在我的問題已修復。 –