我期待从控制器传递一个变量来我已经创建了一个指令:角度:通过变量指令
HTML
<div class="dropup inline-block" ng-repeat="event in video.events">
<event video="video"></event>
</div>
DIRECTIVE
.directive("event", function() {
return {
restrict: "E",
replace: true,
scope:{
video: '=videoObject'
},
template: '<a class="btn btn-default event-margin" ng-style="{color: video.iconColor, float: floatEvents}" type="button" data-title="{{event.title}}"><span class="fa fa-fw {{event.icon}}"></span></a>',
link: function(scope, elm, attrs) {
elm
.on('mouseenter', function() {
elm.css('background-color', scope.video.color);
elm.css('color','#FFFFFF');
})
.on('mouseleave', function() {
elm.css('background-color','#FFFFFF');
elm.css('color', scope.video.color);
});
}
};
问题是当我在返回的字典中添加范围属性它停止工作。
所以这个想法是当我在元素上传递鼠标时改变元素的颜色,值为video.color,这是控制器$范围内的一个变量。
我一直在寻找其他职位的答案,但他们不工作:
- Passing variable from controller scope to directive
- Easiest way to pass an AngularJS scope variable from directive to controller?
您需要在代码中使用'scope:{video:'='}'它会搜索'video-object'属性。 – jcubic
您也可以使用范围:{video:'= video'} – Knu8
它不工作,我测试过了,但我不知道为什么不工作 – albert