Ole*_*sov 3 javascript angularjs cordova angularjs-directive
我想要实现的目标:将cordova本机处理程序包含在angular指令中
我想使用指令包装器为Cordova的本机事件实现处理程序(以便监听body load事件).
我有以下指令样板:
angular.module('vitaApp')
.directive('cordovaNative', function () {
return {
restrict: 'A',
compile: function (element, attrs, transcludeFn) {
alert('compile fired');
element.bind('load', function(){
alert('load occured');
});
},
link: function postLink(scope, element, attrs) {
alert('link fired');
element.bind('load', function(){
alert('load occured');
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
以下列方式实例化:
<body ng-app="vitaApp" ng-controller="metaCtrl" ng-swipe-right="showMenu()" ng-swipe-left="hideMenu()" cordova-native>
Run Code Online (Sandbox Code Playgroud)
编译cordovaNative指令的函数会触发,但链接函数却没有.
它可能与ng-swipe指令有关(例如'{terminal:true}')?
注:我不尝试使用compile并link在一起,我想证明,非他们的作品申请使用的目的,load单独的事件.
您不能在指令中同时具有编译和链接功能.如果使用编译,则应返回一个函数,该函数本身就是一个链接函数.所以上面的代码变成:
compile: function (elem, attrs, transcludeFn) {
alert('compile fired');
return function(scope, element, attrs) {
alert('link fired');
element.on('load', function(){
alert('load occured');
});
}
},
Run Code Online (Sandbox Code Playgroud)
更新:由于指令链接函数在加载元素后运行(大多数情况下),element load可能不需要在指令链接函数内添加事件处理程序.
| 归档时间: |
|
| 查看次数: |
1349 次 |
| 最近记录: |