[英]unable to find the scope of dynamically created fields in angularjs
I am trying to create the form in which user can dynamically add the input field by pressing the button. 我正在尝试创建一种表单,用户可以在其中通过按下按钮来动态添加输入字段。 but I am not able to fetch the value of that input field to controller? 但我无法获取该输入字段的值给控制器? The following is my form: 以下是我的表格:
<div ng-repeat="skill in skill_set">
<label>Skill-name</label>
<input type="text"name="" ng-model="skill.sName" placeholder="enter your skills here">
<button class="remove" ng-show="$last" ng-click="removeSkill()">-</button>
</div>
<button type="button" ng-click="addNewSkill()">Add more Skills</button>
<a ui-sref="/" ng-click="createMeetup()"> Next Section </a>
my controller is defined as follows: 我的控制器定义如下:
var app = angular.module('meetupApp', ['ngResource','ui.router']);
app.controller('mainCrl', ['$scope', '$resource', '$location', function ($scope, $resource, $location) {
$scope.skill_set = [{id: 'skill1'}];
$scope.addNewSkill = function() {
var newItemNo = $scope.skill_set.length+1;
$scope.skill_set.push({'id':'skill'+newItemNo});
};
$scope.removeSkill = function() {
var lastItem = $scope.skill_set.length-1;
$scope.skill_set.splice(lastItem);
};
$scope.createMeetup = function () {
var employee = new Employee();
console.log("the skill set is"+ $scope.skill_set.sName);
employee.skills =[{skill_name: $scope.skill_set.sName}] ;
employee.$save();
};
}]);
Employee is the mongodb document where skills is array of skill_name. 员工是mongodb文档,其中技能是skill_name的数组。 When I am consoling the skill set. 当我安慰技能时。 it showing me "the skill set is undefined" how do I get the value from input field to controller? 它向我显示“技能集未定义”,如何从输入字段到控制器获取值? Small piece of help is really appreciated!!!! 小小的帮助真的很感激!!!
You are filling sName during iteration. 您正在迭代中填充sName。 So if you wan't to reach a skill name, the code would look like $scope.skill_set[iteration].sName
因此,如果您不想获取技能名称,则代码应类似于$scope.skill_set[iteration].sName
If you wan't an array of skill name, make a loop over skill_set to create the proper array. 如果您不需要技能名称数组,请在skill_set上循环以创建适当的数组。
But it would be easier to manipulate your $scope with ng-model="skill.skill_name"
但是使用ng-model="skill.skill_name"
操作$ scope会更容易
And assign employee.skills = $scope.skill_set;
并分配employee.skills = $scope.skill_set;
You did add an id to the model, to keep it clean, you must avoid using Id property, so add new items with: $scope.skill_set.push({'skill_name':''+});
您确实在模型中添加了一个ID,以保持其清洁,必须避免使用Id属性,因此请添加新项: $scope.skill_set.push({'skill_name':''+});
$scope.skill_set
should look like $scope.skill_set
应该看起来像
[
{'skill_name' : "skillname1"},
{'skill_name' : "skillname2"},
{'skill_name' : "skillname3"}
]
Or if you want an array with only skill names 或者,如果您只需要技能名称的数组
var skillNames = [];
for (skill in $scope.skill_set)
{
skillNames.push(skill.skill_Name);
}
employee.skills = {'skill_name' : skillNames};
And employee.skills
would look like employee.skills
技能看起来像
{ 'skill_name' : [ "skillname1", "skillname2", "skillname3" ]}
V2 : code clean V2:代码干净
But it would be easier to manipulate your $scope with ng-model="skill"
但是使用ng-model="skill"
操作$ scope会更容易
And assign employee.skills = {'skill_name' : $scope.skill_set};
并分配employee.skills = {'skill_name' : $scope.skill_set};
You did add an id to the model, to keep it clean, you must avoid using Id property, so add new items with: $scope.skill_set.push({''});
您确实在模型中添加了一个ID,以保持其清洁,必须避免使用Id属性,因此请添加新项: $scope.skill_set.push({''});
And employee.skills
would look like employee.skills
技能看起来像
{ 'skill_name' : [ "skillname1", "skillname2", "skillname3" ]}
I keep first solution, because i'm not sure of what kind of data format you need. 我保留第一个解决方案,因为我不确定您需要哪种数据格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.