AngularJS将属性值作为参数传递给ng-change调用的函数

时间:2014-10-18 23:53:44

标签: javascript html angularjs angularjs-directive angularjs-scope

我似乎无法理解如何将其拉下来。我有一个如下所示的指令:

.directive('seqWidget', ['Sequence', function(Sequence){
    return {        
        restrict: 'E',
        scope: {
            placeholder: '@',
            option: '@'
        },
        template: '<fieldset><legend data-ng-transclude></legend><input type="text" placeholder = {{placeholder}} autofocus data-ng-model="index" data-ng-change="retrieve({{option}});"/>{{output}}</fieldset>',
        replace: true,
        transclude: true,
        link: function ($scope, $elem, $attr){

            $scope.retrieve = function(key){
                //var option = $attr.option;
                console.log(key);
            }
        }
    }
}]);

我的HTML就是这样:

<seq-widget placeholder="Index 0-400" option="accurate">Which index number would you like to gain accuracy on?</seq-widget>

我已经尝试了几种其他方法来实现基于属性值更改函数调用的动态方法。我会用'&amp;'前缀,但我希望在输入更改时随时触发此功能。是否有实用的方法来实现我想要做的事情?或者我是否需要使用jQuery来说出$('input')。on('change',function(){});在我的链接功能?

1 个答案:

答案 0 :(得分:2)

在设置文本绑定option时,您不必传递option: '@'它已在范围内。

所以这样做: -

        $scope.retrieve = function(key){
            console.log($scope.option);
        }

如果删除插值,它也可以工作,您不必在表达式中插入范围变量。

 data-ng-change="retrieve(option);"