在下面的HTML中,我想从控制器中的范围变量注入'src'值。图像源与指令内的范围变量不绑定
HTML:
<customdir filterby="name" src="imgName"></customdir>
的$ scope.imgName变量在控制器具有的图像路径。
javascipt的:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($http, $scope) {
$scope.name = 'ready';
$scope.imgName = '~/Content/arrow-right-xxl.png';
)};
app.directive('customdir', function ($compile) {
var getTemplate = function (filter) {
switch (filter) {
case 'ready': return '<div class="col-lg-1" id="ready">' +
'<img src={{src}} style="height: 35px; width: 35px; margin-left: 0px; margin-top: 35px" />' +
'</div>';
default: return '<input type="text" ng-model="filterby" />';
}
}
return {
restrict: 'E',
scope: {
src: '@',
filterby: "="
},
link: function (scope, element, attrs) {
var el = $compile(getTemplate(scope.filterby))(scope);
element.replaceWith(el);
}
};
});
使用'img'标签 –
中的'ng-src'和上面的代码,发生了什么?模板是否使用'input'元素进行渲染? –
是它对输入元素的渲染,并且它能够使用** $ scope.name **进行绑定。但不适用于imgName ** img元素。 – Prashanth