gpt4 book ai didi

angularjs - 动态添加指令

转载 作者:行者123 更新时间:2023-12-02 01:30:31 26 4
gpt4 key购买 nike

我正在生成包含问题集合的报告。

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 是文本区域

<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。

最佳答案

k 之后,您的解决方案将不起作用是一个局部变量,对于 $compiler 是不可访问的服务。

一个解决方案是让你的指令使用 ngRepeatngIf通过模板生成最终布局:

app.directive('question', function ($compile) {
return {
transclude: true,
restrict: 'E',
templateUrl: 'app/directives/questions.html',
scope: {
contents: '='
}
};
});

还有 app/directives/questions.html :

<div ng-repeat="question in contents">
<accstextbox ng-if="question.questiontype == 1" data="question"></accstextbox>
<accstextarea ng-if="question.questiontype == 2" data="question"></accstextarea>
</div>

因为这是一个非常小的模板,您可以将它添加到 template指令的配置参数,而不是通过 templateUrl 从服务器加载它.

希望对您有所帮助!

关于angularjs - 动态添加指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34559159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com