Javascript 从视图中的动态文本框向AngularJS控制器发送数据
我有一个下拉列表,当用户从下拉列表中选择任何选项时,每个选项都会显示一个文本框。 HTML- 但这使得数据的值未定义。Javascript 从视图中的动态文本框向AngularJS控制器发送数据,javascript,c#,jquery,angularjs,Javascript,C#,Jquery,Angularjs,我有一个下拉列表,当用户从下拉列表中选择任何选项时,每个选项都会显示一个文本框。 HTML- 但这使得数据的值未定义。 那么还能做些什么呢?使用AngularJS的数据驱动方法,从jQuery方法转移到问题上。您可以遵循下面的解决方案 让我们先看看你的问题 您有一个要显示的标签/标记列表 用户输入的文本必须与用户在选择选项中选择的标签/标签相关联 如果未选择“选择”菜单中的任何选项,则不会显示文本输入字段 一旦用户选择标签并输入相应的标签,然后按Submit。您希望将数据保存到后端/数据库 让我
那么还能做些什么呢?使用AngularJS的数据驱动方法,从jQuery方法转移到问题上。您可以遵循下面的解决方案 让我们先看看你的问题
angular.controller('testController',['$scope','$http','$location',function($scope,$http,$location){
$scope.optionsList = ['N1','N2','N3','N4','N5']; //List of options to be displayed in select
$scope.selectedOption = 'Select Tags'; //Variable to store the selected option
$scope.textFilled = ''; //this model will store the text entered by the user
$scope.showTextField = false;// this model will decide when to show the text-field
$scope.switchTextFieldStates = function(){
if($scope.selectOptions != 'Select Tags'){
$scope.showTextFields = true;
}else {
$scope.showTextFields = false;
}
$scope.textFilled = ''; //This will ensure that if user changes the option then previously filled data is cleared out of the model
}
$scope.saveData = function(){
console.log($scope.selectedOption,$scope.textFilled);
//Do whatever you want to do here with the data you got
//Reset the state of the view
$scope.showTextFields = false;
$scope.textFillled = '';
$scope.selectedOptions = 'Select Tags';
}
}];
让我们为这个问题创建一个合适的HTML模板
<select ng-options="item as item in optionsList" ng-model="selectedOption" ng-change="switchTextFieldStates()"></select>
<div id="holder" ng-show="showTextFields"><input type="text" ng-model="textFilled"/></div>
<input type="submit" ng-click="saveData()"/>
使用AngularJS的数据驱动方法,从jQuery方法转移到问题上。您可以遵循下面的解决方案 让我们先看看你的问题
angular.controller('testController',['$scope','$http','$location',function($scope,$http,$location){
$scope.optionsList = ['N1','N2','N3','N4','N5']; //List of options to be displayed in select
$scope.selectedOption = 'Select Tags'; //Variable to store the selected option
$scope.textFilled = ''; //this model will store the text entered by the user
$scope.showTextField = false;// this model will decide when to show the text-field
$scope.switchTextFieldStates = function(){
if($scope.selectOptions != 'Select Tags'){
$scope.showTextFields = true;
}else {
$scope.showTextFields = false;
}
$scope.textFilled = ''; //This will ensure that if user changes the option then previously filled data is cleared out of the model
}
$scope.saveData = function(){
console.log($scope.selectedOption,$scope.textFilled);
//Do whatever you want to do here with the data you got
//Reset the state of the view
$scope.showTextFields = false;
$scope.textFillled = '';
$scope.selectedOptions = 'Select Tags';
}
}];
让我们为这个问题创建一个合适的HTML模板
<select ng-options="item as item in optionsList" ng-model="selectedOption" ng-change="switchTextFieldStates()"></select>
<div id="holder" ng-show="showTextFields"><input type="text" ng-model="textFilled"/></div>
<input type="submit" ng-click="saveData()"/>
当您的页面完成加载时,链接阶段就结束了,因此我认为您使用JQuery动态创建的输入无法与Angular绑定。您可以实现您的需求,但这根本不是直线和角度的推荐方式。链接阶段在您的页面完成加载时完成,因此我认为您使用JQuery动态创建的输入无法与角度绑定。您可以实现您的需求,但它不会是直线和角度的推荐方式。谢谢!这很有效。但这会一个接一个地保存数据。如果我想同时保存多个文本字段中的值,该怎么办?谢谢!这很有效。但这会一个接一个地保存数据。如果要同时保存多个文本字段中的值,该怎么办?
angular.controller('testController',['$scope','$http','$location',function($scope,$http,$location){
$scope.optionsList = ['N1','N2','N3','N4','N5']; //List of options to be displayed in select
$scope.selectedOption = 'Select Tags'; //Variable to store the selected option
$scope.textFilled = ''; //this model will store the text entered by the user
$scope.showTextField = false;// this model will decide when to show the text-field
$scope.switchTextFieldStates = function(){
if($scope.selectOptions != 'Select Tags'){
$scope.showTextFields = true;
}else {
$scope.showTextFields = false;
}
$scope.textFilled = ''; //This will ensure that if user changes the option then previously filled data is cleared out of the model
}
$scope.saveData = function(){
console.log($scope.selectedOption,$scope.textFilled);
//Do whatever you want to do here with the data you got
//Reset the state of the view
$scope.showTextFields = false;
$scope.textFillled = '';
$scope.selectedOptions = 'Select Tags';
}
}];
<select ng-options="item as item in optionsList" ng-model="selectedOption" ng-change="switchTextFieldStates()"></select>
<div id="holder" ng-show="showTextFields"><input type="text" ng-model="textFilled"/></div>
<input type="submit" ng-click="saveData()"/>