我试图在我的指令中访问控制器范围但无法访问。我创建了一个指令'dynamicElement'来创建新的HTML元素,它的工作正常,但在我的指令中,我无法访问我的控制器范围。
我的代码:控制器
.controller('DashCtrl', function($scope, SurveyQuest, $state, $stateParams, $sce) {
var QuestData = SurveyQuest.all();
if($state.params.qid == ''){
var questionOrder = 1;
}else{
var questionOrder = $state.params.qid;
}
//console.log(QuestData.data[paramQid]);
//console.log(QuestData);
//console.log(QuestData.data.length);
var paramQid;
angular.forEach(QuestData.data, function(value, key) {
if(value.question_order == questionOrder){
paramQid = key;
}
//console.log(value.question_order+' '+key);
});
console.log(paramQid);
console.log(QuestData.data);
$scope.question = $sce.trustAsHtml(QuestData.data[paramQid].question_text);
$scope.description = $sce.trustAsHtml(QuestData.data[paramQid].question_desc);
switch(QuestData.data[paramQid].question_type){
case'radio':
var radioLength = QuestData.data[paramQid].answers.length;
var finalAnswers = '';
for(var i = 0; i < radioLength; i++){
finalAnswers+= '<label><input type="radio" ng-click="clickMe($event)" data-id="'+QuestData.data[paramQid].answers[i].option_next+'" name="'+QuestData.data[paramQid].question_id+'" value="'+QuestData.data[paramQid].answers[i].option_value+'" /> '+QuestData.data[paramQid].answers[i].option_text+'</label>';
}
$scope.htmlString = finalAnswers;
break;
.............
$scope.next = function(qid){
console.log(qid);
$state.go('tab.dash',{'qid':qid});
}
查看代码:
<dynamic-element message='htmlString' ng-bind-html="ans"></dynamic-element>
<div class="buttons" style="text-align:center; margin-top:10%;">
<button class="button button-positive">
Prev
</button>
<button class="button button-assertive" >
Stop
</button>
<button class="button button-balanced" ng-click="next({{qid}})">
Next
</button>
</div>
指令代码:
.directive('dynamicElement', ['$compile', function ($compile) {
return {
restrict: 'E',
scope: {
message: "="
},
replace: true,
link: function(scope, element, attrs) {
var template = $compile(scope.message)(scope);
element.replaceWith(template);
},
controller: ['$scope', function($scope) {
$scope.clickMe = function(item){
$scope.qid = item.currentTarget.getAttribute("data-id"); //NOT WORKING
console.log(item.currentTarget.getAttribute("data-id"));
};
}]
}
}])
请建议我......谢谢