如何在指令中使用控制器变量。我在指令范围内使用了 popoverHtml 但是当我添加类型这样的类型时不起作用:
像这样:scope:{popoverHtml:'@',输入:@},
我的HTML是:
good <input type='radio' name='type' value='good' data-ng-model='type'
data-ng-change='change(type)' />
bad <input type='radio' name='type' value='bad' data-ng-model='type'
data-ng-change='change(type)' />
<next-level id='pop' popover-html='{{typeMessage}}'></next-level>
我的控制器是:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.typeMessage = 'PopOverTest';
$scope.type = false;
$scope.change = function(type){
if(type == 'good'){
$scope.typeMessage = 'very good';
}else if(type == 'bad'){
$scope.typeMessage = 'very bad';
}
};
});
我的指示:
app.directive('nextLevel', function () {
return {
restrict: 'EA',
scope:{ popoverHtml:'@'},
template: '<a ui-sref="register" tabindex="0" linkdisabled="{{type}}"
class="btn btn-block btn-success ng-class: {disabled: !(type)}" role="button" >next</a>',
link: function (scope, el, attrs){
$(el[0]).popover({
trigger: 'click',
html: true,
toggle:'popover',
title: 'notice !!',
content: scope.popoverHtml, // Access the popoverHtml html
placement: 'bottom'
});
attrs.$observe('popoverHtml', function(val){
$(el[0]).popover('hide');
var popover = $(el[0]).data('bs.popover');
popover.options.content = val;
console.log(popover);
})
}
};
});