我正在尝试为 AngularJS 编写一个 imgix 指令。这是我的代码:
MetronicApp
.directive('imgix', function() {
return {
replace: true,
scope: {
url: '='
},
restrict: "A",
template: "<img class='thumbnail inline' width={{w}} height={{h}} src='https://mysite.imgix.net/{{url}}?h={{h}}&w={{w}}&fit=crop'>",
link: function(scope, elm, attrs) {
function ctrl(value, mode) {
// check inputs
if ((value === null) || (value === undefined) || (value === '')) {
// let's do nothing if the value comes in empty, null or undefined
return;
}
scope.h = attrs.height || 50;
scope.w = attrs.width || 50;
scope.url = value;
}
// by default the values will come in as undefined so we need to setup a
// watch to notify us when the value changes
scope.$watch(attrs.url, function(value) {
ctrl(value, 'url');
});
}
};
});
在 html 中,我有 2 张图片:
<img imgix data-height="200" data-width="200" url="theme.options.homepageBackground" />
<img imgix data-height="200" data-width="200" url="theme.options.menuBackground" />
但结果如图所示:
我不明白出了什么问题。有什么关于范围的吗?