AngularJS を使用して、質問をフォームにテンプレート化するディレクティブを作成しようとしています。特定の種類の質問について、特定の範囲内の各数値のラジオ ボタンを作成したいと考えています。下限と上限が指定された数値の配列を返す外部関数があります。
このタイプの質問をテンプレート化するために、この外部関数を ng-repeat と一緒に使用するにはどうすればよいですか? これまでに試したコードは次のとおりです...
HTML:
<qtnrange qtn-variable="cxnTestVarA" first-num="1" last-num="5">
This is a test question. Pick a number between 1 and 5.</qtnrange>
<hr>
You picked {{cxnTestVarA}}.
JS:
var module = angular.module('app', [])
.directive('qtnrange', function() {
return {
scope: {
qtnVariable: '=',
firstNum: '=',
lastNum: '=',
bounds: '&range',
},
template:
'<div class=question>' +
'<label ng-transclude></label>' +
'<label class="radio inline" ng-repeat="iter in bounds(firstNum, lastNum)"><input type="radio" value="{{iter}}" ng-model="qtnVariable">{{iter}} </label>' +
'<hr></div>',
restrict: 'E', // must be an HTML element
transclude: true,
replace: true,
};
});
var range = function(start, end, step) {
... function that returns an array []
}