绑定angularJS中数组对象的特定值

时间:2017-02-23 10:31:22

标签: angularjs arrays object

testing:{"name":"Mohan,Rahul,Kanal,Rajesh,Gokul,Ramesh"}  

假设我想要使用角度ng-repeat将前三个名称绑定到标签中。这是可能的。帮助我。提前谢谢

2 个答案:

答案 0 :(得分:0)

请查看以下代码,以解决您的问题。

<!DOCTYPE html>
    <html>
    <body>
        <div ng-app="myApp" ng-controller="sizeCtrl">
            <ul>            
            <li ng-repeat="x in arr | limitTo:3">{{x}}</li>
            </ul>
        </div>
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
        <script>
        var app = angular.module('myApp', []);
        app.controller('sizeCtrl', function($scope) {
            $scope.arr = [];
            $scope.cars = {name: "Audi,BMW,Dodge,Fiat,Ford,Volvo"};
            $scope.arr = $scope.cars.name.split(",");
        });
        </script>
    </body>
</html>

答案 1 :(得分:0)

您可以使用ng-repeat

  <label>
    <span ng-repeat="name in testing.name.split(',') | limitTo:3">
      {{name}}<span ng-show="$index<2">,</span>
    </span>
  </label>

另一种解决方案:以数组格式显示前三个名称:

{{testing.name.split(',') | limitTo:3}}

另一种解决方案:您可以拆分字符串,然后将前三个连接到另一个范围变量并绑定到它。

var arr = $scope.testing.name.split(',');
$scope.labelStr = "";
for(var i=0; i<3; i++) {
    $scope.labelStr += arr[i] + ',';
}
$scope.labelStr = $scope.labelStr.substr(0, $scope.labelStr.length-1);