如何在AngularJS

时间:2016-11-21 00:16:44

标签: angularjs

我有一个微小的指令,可以模拟元素点击时的'keydown'事件。它完全正常,但我想通过直接从视图传递'keydown'值来创建一个更通用的。

请你帮我理解这个功能在角度方面是如何工作的,因为其中一些仍然有点令人困惑:)提前感谢。

我的指示:

app.directive('simulateKeydown', [function(){
    return function(scope, element, attr){
        var e = jQuery.Event("keydown");
        e.which = 36;
        element.bind('click', function(){
            $(element).trigger(e);
        })

    }
}])

当前观点:

<div simulate-keydown></div>

期望的观点:

<div simulate-keydown="36"></div>

2 个答案:

答案 0 :(得分:1)

for scanner.Scan() { // loop through each url in the file // send each url to golang HTTPrequest go HTTPrequest(scanner.Text(), channel, &wg) } fmt.Println(<-channel) wg.Wait() 参数(此处attr包含您的所有属性。只需使用它。

return function(scope, element, attr))

然后您可以根据需要使用该指令

app.directive('simulateKeydown', [function(){
    return function(scope, element, attr){
        var e = jQuery.Event("keydown");
        e.which = parseInt(attr.simulateKeydown); // the value you gave it
        element.bind('click', function(){
            $(element).trigger(e);
        })
    }
}])

答案 1 :(得分:1)

<script>

var app = angular.module("myApp", []);
app.directive('simulateKeydown', [function(){
    return function(scope, element, attr){
        var e = jQuery.Event("keydown");
        e.which = attr.key;
        element.bind('click', function(){
            $(element).trigger(e);
        })

    }
}])

</script>
<div ng-app="myApp">
    <div simulate-keydown data-key="36"></div>
</div>
相关问题