有人可以指導我嗎?當我將$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
});
}
}
檢查[ngRepeat'文檔](https://docs.angularjs.org/api/ng/directive/ngRepeat)。每個'ngRepeat'循環都有自己的範圍。如果情況並非如此,那麼你的'addDetail'方法如何知道你所指的是'nameStaff'? – Lex