我创建了一个小指令:
HTML:
<my-directive attr1="{{ data1 }}" attr2="{{ data2 }}"></my-directive>
指令:
app.directive('myDirective', ['$rootScope',
function ($rootScope) {
return {
restrict: 'E',
link: function (scope, element, attrs) {
// do something...
}
};
}]);
现在我想添加一个我在网上找到的插件:https://github.com/thenikso/angular-inview
只有当该元素位于视口(viewport)中时,我才希望对该元素“执行某些操作”。
所以我修改了指令,如 View 中文档中所述:
<my-directive attr1="{{ data1 }}" attr2="{{ data2 }}" in-view="$inview">
问题是我不知道如何获取bool
应该生成...
attrs.inView
返回 string
:"$inview"
.
我做了几次测试,但没有任何效果。
如何才能正确添加并让它正常工作?
该指令位于 ng-repeat
内.
谢谢
编辑:
<my-directive attr1="{{ data1 }}" attr2="{{ data2 }}" scope1="someScope" in-view="checkStatus($inview)"></my-directive>
<小时/>
return {
restrict: 'E',
scope: {
scope1: '='
},
link: function (scope, element, attrs) {
// do something...
}
最佳答案
我认为您需要使用一个函数,然后处理由 inview 指令传递的事件。尝试如下所示的操作。
app.directive('myDirective', ['$rootScope',
function ($rootScope) {
return {
restrict: 'E',
link: function (scope, element, attrs) {
// do something...
scope.checkStatus = function(status) {
console.log(status);
if(status) {
// do something here
}
}
}
};
}]);
<my-directive attr1="{{ data1 }}" attr2="{{ data2 }}" in-view="checkStatus($inView)"></my-directive>
关于javascript - 了解如何在指令中使用指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36067141/