我正在生成一份包含问题集合的报告。
app.controller('reportCtrl', ['$scope','$stateParams', function ($scope, $stateParams) {
$scope.questions: [
{ questionkey: 1, questiontext: 'Type', questiontype: 1 , questionmodel:'accsType' },
{ questionkey: 2, questiontext: 'Reported By', questiontype: 1, questionmodel: 'accsReportedBy' },
{ questionkey: 3, questiontext: 'Time and Date', questiontype: 6, questionmodel: 'accsCurrentDate' },
{ questionkey: 4, questiontext: 'Address', questiontype: 2, questionmodel: 'accsAddress' },
{ questionkey: 5, questiontext: 'Coordinates', questiontype: 6, questionmodel: 'accsCoordinates' },
{ questionkey: 6, questiontext: 'Blank', questiontype: 1, questionmodel: 'accsBlank1' },
{ questionkey: 7, questiontext: 'Blank', questiontype: 1, questionmodel: 'accsBlank2' },
{ questionkey: 8, questiontext: 'Blank', questiontype: 1, questionmodel: 'accsBlank3' },
{ questionkey: 9, questiontext: 'Blank', questiontype: 1, questionmodel: 'accsBlank4' },
{ questionkey: 10, questiontext: 'Details of Survey', questiontype: 2, questionmodel: 'accsDetailsSurvey' },
{ questionkey: 11, questiontext: 'Photos', questiontype: 5, questionmodel: 'accsPhotos' }
];
}]);
我创建了一个自定义指令来根据问题类型绘制问题例如问题类型 1 是文本框类型 2 是 textarea
<question contents="questions"></question>
app.directive('question', function ($compile) {
return {
transclude: true,
restrict: 'E',
scope: {
contents: '='
},
link: function (scope, element, attrs) {
angular.forEach(scope.contents, function (k, v) {
var ele;
switch (parseInt(k.question.questiontype)) {
case 1:
ele = $compile("<accstextbox data='k.question'></accstextbox>")(scope);
break;
case 2:
ele = $compile("<accstextarea data='k.question'></accstextarea>")(scope);
break;
}
element.append(ele);
});
}
};
});
我为每种问题类型创建了一个指令
app.directive('accstextbox', [function () {
return {
restrict: 'E',
templateUrl: 'app/directives/questions/textbox.html',
link: function (scope, element, attrs) {
console.log(scope.data); // undefined
},
scope:{
data: '='
}
};
}]);
app.directive('accstextarea', [function () {
return {
restrict: 'E',
templateUrl: 'app/directives/questions/textarea.html',
link: function (scope, element, attrs) {
console.log(scope.data); // undefined
},
scope: {
data: '='
}
};
}]);
当我动态添加这些指令时,我通过属性传递数据对象。该数据对象在子指令范围内未定义。我第一次在我的项目中使用 angularjs。