javascript - 了解如何在指令中使用指令

标签 javascript angularjs

我创建了一个小指令:

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/

相关文章:

javascript - 覆盖与父级溢出时对齐的 div

javascript - 当用户单击通知 Div 之外的任意位置时,使通知 Div 淡出

javascript - WordPress 分页 <a> 链接单击并更改为 "active"但不重定向

javascript - 获取表单值以创建自定义 URL

javascript - knexfile.js 不读取 Dotenv 变量

AngularJS + 指令 : Multiple Transcluded Elements

javascript - 通过变量 angularjs 选择我范围内的一个项目

javascript - ng-model 在模态中没有绑定(bind)两种方式

backbone.js 和/或 knockout.js 中的 AngularJS 示例

angularjs - 根据AngularJS中选择框中选定的城市绘制 map