Javascript 如何获取指令内输入字段的值
我有一个带有输入字段的指令Javascript 如何获取指令内输入字段的值,javascript,angularjs,Javascript,Angularjs,我有一个带有输入字段的指令,其中有一个select。如何在指令函数中键入或选择值 使用my指令的html: <div class="certFull"> <stu-directive obj ="certObj" ng-model="stuDirModel"></stu-directive> <div class="addDir col-md-12 mg"> 伙计,你没有在html中提到你的指令,而且你的指令是基于元素的。您需要将指令用作ht
,其中有一个select
。如何在指令函数中键入或选择值
使用my指令的html:
<div class="certFull">
<stu-directive obj ="certObj" ng-model="stuDirModel"></stu-directive>
<div class="addDir col-md-12 mg">
伙计,你没有在html中提到你的指令,而且你的指令是基于元素的。您需要将指令用作html中的元素 请仔细阅读这些链接可能会对您有所帮助。 请参阅代码片段“我能够在指令中获取元素”
var-app=angular.module('app',function(){});
应用指令('stuDirective',函数(){
返回{
限制:'E',
是的,
//templateUrl:'htmlFiles/stuDirective.html',
链接:功能链接(范围、元素、属性){
scope.Details=函数(){
console.log(scope.selectStudent+“”+scope.studentMark);
};
},
控制器:['$scope','$timeout',函数($scope,$timeout){
}]
};
});代码>
学名
斯图1
斯图2
斯图3
斯图4
学生成绩
呼叫目录
在ng车型中始终使用
在指令中定义一个变量,然后在ng model
uiRouteApp.directive('studdirective',function(){
返回{
限制:'E',
//范围:{
//外部对象:'=obj'
//},
是的,
templateUrl:'htmlFiles/stuDirective.html',
链接:功能链接(范围、元素、属性){
//如何访问指令中的输入字段值
},
控制器:['$scope','$timeout',函数($scope,$timeout){
var studirectivetrl=此;
studirectivetrl.selectStudent='';
studirectivetrl.log=函数(){
console.log(this.selectStudent);
}
}],
控制器:'studirectivetrl'
}
})
斯图1
斯图2
斯图3
斯图4
在HTML中,您必须将指令用作元素,并在其中添加一个属性,该属性包含您应在指令中使用的值,例如:
<stu-directive selected="selectStudent"></stud-directive>
uiRouteApp.directive('stuDirective', function () {
return {
restrict: 'E',
scope: {
selected: '='
},
transclude: true,
templateUrl: 'htmlFiles/stuDirective.html',
link: function link(scope, element, attrs) {
console.log(scope.selected);
}]
}
您需要这样的事件:(或者如果您不想使用event,请使用$watch)
var-app=angular.module('app',function(){});
应用指令('stuDirective',函数(){
返回{
限制:'E',
是的,
//templateUrl:'htmlFiles/stuDirective.html',
链接:功能链接(范围、元素、属性){
scope.Print=function(){
console.log(scope.selectStudent+“”+scope.studentMark);
};
},
控制器:['$scope','$timeout',函数($scope,$timeout){
}]
};
});代码>
学名
斯图1
斯图2
斯图3
斯图4
学生成绩
呼叫目录
angular.module(“myApp”,[])
.指令('stuDirective',函数(){
返回{
限制:'E',
范围:{
选择学生:“@”,
学生标记:“@”
},
模板:'label for=“studentNameId”>学生姓名Stu1 Stu2Stu3 Stu4学生标记',
链接:功能链接(范围、元素、属性){
},
控制器:函数($scope,$timeout){$scope.$watch('[studentMark,selectStudent]',函数(value,value1){
console.log(值)
console.log(值1)
})
}
}
})
您看到的html就是指令。我还没有发布使用该指令的html。那么我们如何帮助您呢。如果你不发布完整的代码,我们如何理解代码并为你提供解决方案对不起,我在上面提到过。我想那就够了。我已经编辑了我的问题嗯,我如何获得输入框中键入的文本值,该输入框位于directivePut值中options@BurakAkyıldız。之后如何访问?请避免只使用代码回答。相反,解释你的代码是做什么的,以及它如何/为什么可以解决这个问题。
<stu-directive selected="selectStudent"></stud-directive>
uiRouteApp.directive('stuDirective', function () {
return {
restrict: 'E',
scope: {
selected: '='
},
transclude: true,
templateUrl: 'htmlFiles/stuDirective.html',
link: function link(scope, element, attrs) {
console.log(scope.selected);
}]
}
angular.module("myApp",[])
.directive('stuDirective', function () {
return {
restrict: 'E',
scope:{
selectStudent:'@',
studentMark:'@'
},
template: '<div class="container-fluid stuDirectiveClass mg"><div class="rows"><div class="col-md-12 mg"><div class="form-group"><div class="rows"><div class="col-md-6">label for="studentNameId">Student name</label></div> <div class="col-md-6"><select class="form-control" ng-model="selectStudent"><option>Stu1</option> <option>Stu2</option><option>Stu3</option> <option>Stu4</option></select></div></div></div></div><div class="col-md-12"><div class="form-group mg"><div class="rows"> <div class="col-md-6"><label for="studentNameId">Student mark</label></div><div class="col-md-6"><input type="text" class="form-control" ng-model="studentMark" placeholder="Student mark" /></div></div></div></div></div>',
link: function link(scope, element, attrs) {
},
controller: function ($scope,$timeout) { $scope.$watch('[studentMark,selectStudent]',function(value,value1){
console.log(value)
console.log(value1)
})
}
}
})