【问题标题】:ngRepeat based on the selectionngRepeat 基于选择
【发布时间】:2016-09-19 13:40:47
【问题描述】:

我想将元素的数量更改为选择框给出的数量。 我的代码:

index.php

<select class="form-control-static" ng-model="fieldSelect" ng-change="showSelectValue(fieldSelect)">
    <option ng-repeat="i in getNumber(number) track by $index" value="{{$index+1}}">{{$index+1}}</option>
</select>

index.php

<div ng-repeat="t in getTimes(1) track by $index")>
    Hello!
</div>

default.js

$scope.showSelectValue = function(fieldSelect) {
    contentFields = fieldSelect;
    $scope.getTimes();
};

$scope.getTimes=function(n){
    n = contentFields;
    return new Array(n);
};

【问题讨论】:

    标签: javascript jquery angularjs angularjs-scope ng-repeat


    【解决方案1】:

    试试这个工作示例。

    var myApp = angular.module('myApp',[]);
    
    myApp.controller('GreetingController', ['$scope', function($scope) {
      $scope.getTimes=function(n){
       // Parse the value to int
       return new Array(parseInt(n));
    };
    }]);
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="GreetingController">
    <select class="form-control-static" ng-model="fieldSelect" ng-init="fieldSelect=1">
        <option ng-repeat="i in [1,2,3,4,5] track by $index" value="{{$index+1}}">{{$index+1}}</option>
    </select>
      
    <div ng-repeat="t in getTimes(fieldSelect) track by $index")>
        Hello! {{$index}}
    </div>
    </div>

    【讨论】:

      【解决方案2】:

      在你的 html 中

      <div ng-repeat="t in getTimes({{fieldSelect}}) track by $index")>
         Hello!
      </div>
      

      在你的函数中

      $scope.getTimes=function(n){
         // Parse the value to int
         return new Array(parseInt(n));
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-20
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        相关资源
        最近更新 更多