gpt4 book ai didi

angularjs - 从指令链接调用 $http.get

转载 作者:行者123 更新时间:2023-12-02 04:21:07 26 4
gpt4 key购买 nike

我需要从 AngularJS 中的指令链接调用 $http.get 函数,这是我的代码...这是一个自动完成功能。

或者有什么方法可以从指令链接函数调用 Controller 吗?

这是代码

.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请求函数到数据库

代码继续:

                }
});
},
};
})

我是这个编码的新手,请帮忙...

最佳答案

只需将 $http 包含到指令声明中即可稍后调用

.directive('ionSelect',function($http){

关于angularjs - 从指令链接调用 $http.get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30402586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com