我需要从AngularJS中的指令链接调用$http.get
函数,
这是我的代码......这是一个自动完成功能。
或者有没有办法从指令链接函数调用控制器?
以下是代码
.directive('ionSelect',function(){
'use strict';
return{
restrict: 'EAC',
scope: {
label:'@',
labelField:'@',
provider:'=',
ngModel: '=?',
ngValue: '=?',
myFunctionToCall : '='
},
require: '?ngModel',
transclude : false,
replace: false,
template:
'<div class="selectContainer">'
// +'<label class="item item-input item-stacked-label">'
// +'<span class="input-label">{{label}}</span>'
// +'<div class="item item-input-inset">'
+'<label class="item item-input" >'
+'<i class="icon ion-ios-search-strong"></i>'
+'<input id="filtro" type="search" ng-model="ngModel" style="padding: 5px;" ng-value="ngValue" ng-keydown="onKeyDown()"/>'
// +'</label>'
// +'<button class="button button-small button-clear" ng-click="open()">'
// +'<i class="icon ion-chevron-down"></i>'
// +'</button>'
// +'</div>'
+'</label>'
+'<div class="optionList padding-left padding-right" ng-show="showHide">'
+'<ion-scroll>'
+'<ul class="list">'
+'<li class="item" ng-click="selecionar(item)" ng-repeat="item in provider | filter:ngModel" >{{item[labelField]}}</li>'
+'</ul>'
+'</ion-scroll>'
+'</div>'
+'</div>'
,
link: function (scope, element, attrs,ngModel) {
scope.ngValue = scope.ngValue !== undefined ? scope.ngValue :'item';
scope.ontouch1 = function(item){
console.log(item[labelField]);alert(1);
};
scope.selecionar = function(item){
ngModel.$setViewValue(item);
scope.showHide = false;
};
element.bind('click',function(){
element.find('input').focus();
});
scope.open = function(){
scope.ngModel = "";
return scope.showHide=!scope.showHide;
};
scope.onKeyDown = function(){
scope.showHide = true;
if(!scope.ngModel){
scope.showHide = false;
}
}
scope.$watch('ngModel',function(newValue){
if(newValue)
element.find('input').val(newValue[scope.labelField]);
if(newValue[scope.labelField]!== undefined){
alert(newValue[scope.labelField]);
此时我需要将$http.get
请求函数调用到数据库
代码的延续:
}
});
},
};
})
我是新手,请帮助...
答案 0 :(得分:4)
只需在您的指令声明中包含$http
即可稍后调用
.directive('ionSelect',function($http){