有人可以指导我吗?当我将$scope.nameStaff
放入ng-repeat
时,其值为undefined
。我不明白为什么价值为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
});
}
}
答案 0 :(得分:1)
这就是你想要的(虽然,我不知道为什么你会想要这个):
<!-- 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;
}
答案 1 :(得分:0)
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
});
}
}
myApp.controller('MyCtrl', MyCtrl);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-repeat="f in form">
<input type="text" ng-model="$parent.nameStaff" />
<input type="text" ng-model="$parent.idStaff" />
<button ng-click=addDetail()>add
</button>
</div>
{{form}}
</div>
&#13;
您可以使用&#39; $ parent.nameStaff&#39;。