我正在angularjs创建座位预订系统。座位以矩阵形式显示。并且要求是每当我选择一个空位时,img src应该改为选定的座位。
在Angularjs的学习曲线上,我不知道如何实现这一目标。我的HTML显示座位布局是动态生成的。我正在使用指令在运行时嵌入HTML。还可以使用指令来处理图像上的图像src更改。
为此创建了一个plnkr: - http://plnkr.co/edit/EemPUIKglSz6j1zQss4i?p=preview
我的HTML
<body>
<h1>Compile dynamic HTML</h1>
<div ng-controller="MyController">
<textarea ng-model="html"></textarea>
<div dynamic="html"></div>
</div>
</body>
我的控制器
var app = angular.module('app', []);
app.directive('dynamic', function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(attrs.dynamic, function(html) {
ele.html(html);
$compile(ele.contents())(scope);
});
}
};
});
app.directive('change-on-click', function () {
return {
restrict: 'A',
link: function($scope, element,attrs){
element.bind('click', function(){
attrs.$set('src', 'C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_sleeper_selected.jpg');
})
}
}
});
function MyController($scope,$sce) {
$scope.click = function(arg) {
alert('Clicked ' + arg);
}
var str = ["<table frame='box'><tbody>"
, "<tr>"
, "<td><img id=1 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=4 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=8 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=12 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=16 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=20 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=24 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=28 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=32 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=36 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=40 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img id=44 title='Avaliable' change-on-click src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "</tr>"
, "<tr>"
, "<td><img title='Avaliable' change-on-click id=2 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=5 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=9 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=13 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=17 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=21 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=25 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=29 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=33 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=37 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=41 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=45 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "</tr>"
, "<tr>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/no_seat.jpg'/></td>"
, "<td><img title='Not a seat' id=46 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "</tr>"
, "<tr>"
, "<td><img title='Avaliable' change-on-click id=3 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=6 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=10 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=14 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=18 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=22 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=26 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=30 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=34 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=38 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=42 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=47 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "</tr>"
, "<tr>"
, "<td><img id=Mobile src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_unavailable.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=7 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=11 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=15 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=19 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=23 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=27 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=31 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=35 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=39 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=43 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "<td><img title='Avaliable' change-on-click id=48 src='C:/Users/vk0044682/Desktop/dynamic/dynamic/assets/images/ac_semi_sleeper_vacant.jpg'></img></td>"
, "</tr>"
, "</tbody></table>"];
$scope.html = $sce.trustAsHtml(str.join(''));
}
答案 0 :(得分:1)
你的HTML中的属性必须是蛇形的,但在你的指令定义中是驼峰式的:
app.directive('changeOnClick', function () {