最近项目组需要将项目转到从ng1 转到 ng2。看了相关ng2关于指令的文档,可这是一头雾水,知道将$element 对应ng2 ElementRef,在细节还是不知所措,求指教!
Ng1 Directive
.directive("Touchmove", function () {
return {
controller: ["$scope", "$element", function ($scope, $element) {
$element.bind("touchstart", onTouchStart);
function onTouchStart(event) {
event.preventDefault();
$element.bind("touchmove", onTouchMove);
$element.bind("touchend", onTouchEnd);
}
function onTouchMove(event) {
var method = $element.attr("ng-touchmove");
$scope.$event = event;
$scope.$apply(method);
}
function onTouchEnd(event) {
event.preventDefault();
$element.unbind("touchmove", onTouchMove);
$element.unbind("touchend", onTouchEnd);
}
}]
}
});
.directive("Touchend", function () {
return {
controller: ["$scope", "$element", function ($scope, $element) {
$element.bind("touchend", onTouchEnd);
function onTouchEnd(event) {
var method = $element.attr("ng-touchend");
$scope.$event = event;
$scope.$apply(method);
}
}]
}
})
HTML
<div touchend="mRelease()" touchmove="mTouch($event)" ng-click="mTouch($event)" >
<div ng-repeat="c in indexs" style="width:100%;height:{{hIndex}}px;">
{{c}}
</div>
</div>