在typeahead指令上设置范围变量

时间:2015-04-26 15:12:28

标签: javascript angularjs

我有一个自定义指令设置,可以使用typeahead.js和angular,如下所示:

app.directive('typeahead', function(ergastAPI){
    return function(scope, element, attrs){
        ergastAPI.getCityDetails().success(function(data){
            var vals = data.response[0].vals;
            var suggestions = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                local: $.map(vals, function(d) {
                    return {
                        value:d.name,
                        suggest: d
                    }
                })
            });
            suggestions.initialize();
            $(element[0]).typeahead({
                minLength: 1,
                hint: true,
                highlight: true
            },{
                name: "suggestions",
                displayKey: "value",
                templates:{
                    suggestion: function(data){
                        var details = '<div class="suggest-drop"><span class="" style="margin-right:15px;"></span>'+data.value+'</div>';
                        return details;
                    },
                    header: '<div class="suggest-header">Car</div>'
                },
                source: suggestions.ttAdapter()
            }).on('typeahead:selected typeahead:autocompleted', function(e, s, n){                  
                scope.query = s.value;
            });;
        });
    }
});

这在我的html中使用如下:

<input class="input-picker" ng-model="query" typeahead id="type-element" />

我想要做的是,在选择预先输入选项时,将scope.query值设置为等于已选择的值。但是,上述方法似乎不起作用。如何根据此指令中选择的值设置父控制器的范围?

0 个答案:

没有答案