打印hr取决于ng-model值

时间:2017-04-07 16:16:16

标签: angularjs

我有一个输入类型编号如下:

<input type="number"
ng-model="question.numberOfLines" min="1" max="5" />

它存储在我的范围内的对象中:

$scope.question = {
       questionText: "",
       size: "",
       alignment: "",
       color: "",
       numberOfLines: "",
       htmlLines: ""
}

如果numberOfLines(1,2,3,4, etc) htmlLines的变化与<hr />的值相同,我该怎样才能这样做?我无法解决这个问题。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您需要创建一个长度等于您的输入的数组,以便使用ngRepeat指令来处理您的hr:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.question = {
       questionText: "",
       size: "",
       alignment: "",
       color: "",
       numberOfLines: "",
       htmlLines: ""
  };
  $scope.hrArray = [];
  $scope.$watch('question.numberOfLines', function(n, o) {
    $scope.hrArray = [];
    for (var i=0; i<n; i++) {
      $scope.hrArray.push('');
    }
  });
});

plunkr here