在下面的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