gpt4 book ai didi

javascript - 如果我把它放在 ng-repeat 中,为什么 ng-model=$scope.model 未定义?

转载 作者:行者123 更新时间:2023-12-03 06:50:00 25 4
gpt4 key购买 nike

有人可以指导我吗?当我将 $scope.nameStaff 放入 ng-repeat 中时,它的值为 undefined。我不明白为什么该值是未定义

这是我的代码:

<div ng-controller="MyCtrl">
<div ng-repeat="form in form">
<input type="text" ng-model="nameStaff" />
<input type="text" ng-model="idStaff" />
<button ng-click=addDetail()>add
</button>1
</div>
{{form}}
</div>



var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
$scope.form = [{
companyName: "company1",
companyAddress: "company address",
staff: [{
name: "men",
id: "123"
}]
}, {
companyName: "company1",
companyAddress: "company address",
staff: [{
name: "men",
id: "123"
}]
}]
$scope.addDetail = function() {
alert($scope.nameStaff);
$scope.form[0].staff.push({
name: $scope.nameStaff,
id: $scope.idStaff
});
}
}

最佳答案

这就是你想要的(尽管,我不知道你为什么想要这个):

<!-- don't refer to children with the same name as parents -->
<div ng-repeat="f in form">
Staff Name:
<input type="text" ng-model="f.staff.name" /> <br />
Staff Id:
<input type="text" ng-model="f.staff.id" />
<button type="button" ng-click="addStaff(f.staff)" />
</div>

Javascript:

$scope.addStaff = function(staff) {
var staffName = staff.name;
var staffId = staff.id;
}

关于javascript - 如果我把它放在 ng-repeat 中,为什么 ng-model=$scope.model 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37549462/

25 4 0