2017-04-07 49 views
0

我有一個輸入型數字如下:打印小時取決於NG-模型值

<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

你需要創建一個具有等於您輸入的長度,以使用ngRepeat指令來照顧你小時的數組:

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