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

时间:2016-03-17 17:02:30

标签: 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

我希望&#34;做点什么&#34;仅当元素在视口中时才使用我的元素。

所以我修改了指令文档中描述的指令:

<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...
 }

1 个答案:

答案 0 :(得分:1)

我认为你需要使用一个函数,然后处理由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>
相关问题