【发布时间】:2018-09-28 22:23:51
【问题描述】:
总结
我正在使用配置 replace: true 的第三方指令。
我想在用户每次单击按钮时重新编译指令。我已经尝试了几件事没有运气。例如,我尝试将 Cloudinary 指令包装在我自己的指令中,但我似乎无法得到它。任何帮助将不胜感激。
依赖关系
"angular": "1.6.2",
"cloudinary-core": "^2.5.0",
"cloudinary_ng": "^1.1.1",
控制器
$scope.rotate = (leader) => {
leader.cloudinary_angle = {
'0': '90',
'90': '180',
'180': '270',
'270': '0'
}[leader.cloudinary_angle] || '0';
};
查看
<div ng-repeat="leader in leaders">
<a href="#" ng-click="rotate(leader)">Rotate</a>
<cloudimage leader="leader"></cloudimage>
</div>
没有用 #1
angular.module('app').directive('cloudimage', ($compile) => {
return {
restrict: 'E',
replace: false,
scope: {
leader: '='
},
link: (scope, element) => {
let cloudinaryImage = $compile('<cl-image angle="' + scope.leader.cloudinary_angle + '"' +
' crop="fit"' +
' format="jpg"' +
' height="150"' +
' public-id="' + scope.leader.cloudinary + '"' +
' quality="80"' +
' width="150"' +
'></cl-image>'
)(scope);
element.html(cloudinaryImage[0]);
scope.$watch('scope.leader.cloudinary_angle', (cloudinaryImage) => element.html(cloudinaryImage[0]));
}
};
});
没有用 #2
angular.module('app').directive('cloudimage', ($compile) => {
return {
restrict: 'E',
replace: false,
scope: {
leader: '='
},
template: '<cl-image crop="fit" format="jpg" height="150" angle="{{angle}}" public-id="{{id}}" quality="80" width="150"></cl-image>',
link: (scope, element) => {
scope.angle = scope.leader.cloudinary_angle || 0;
scope.id = scope.leader.cloudinary;
}
};
});
没有用 #3
我可以装饰 3rd 方指令以使其成为 replace: false,但这会破坏它的嵌入。
angular.module('app').config(['$provide', function($provide) {
$provide.decorator('clImageDirective', function($delegate) {
var directive = $delegate[0];
directive.replace = false;
return $delegate;
})
}]);
【问题讨论】:
-
cl-image 真的需要replace:真的吗?您可以扩展它并使其替换:false。
-
我也尝试过这种方法,但无法让它发挥作用。我想我可能知道为什么它不起作用。今晚回家后,我会尝试再次装饰指令,如果仍然无法获得它,则发布代码。
-
我可以装饰指令来制作
replace: false,但是当我这样做时,cl-image指令没有正确嵌入并且图像没有显示。 -
我明白了。您发布的代码不包含按钮。基本上,您需要在事件侦听器中执行 $compile()。
-
我从控制器和视图中添加了一些代码。希望它现在更有意义。当我单击“旋转”时,它会在视图中更新
leader.cloudinary_angle。我在指令中对此进行了关注(请参阅“不起作用#1”)。我没有得到新的图像,而是在视图中得到<cloudimage leader="leader" class="ng-isolate-scope">[object HTMLImageElement]</cloudimage>。
标签: angularjs angularjs-directive cloudinary