我正在学习角度。所以阅读许多帖子和文章的角度。这次我不理解代码中的特定区域。 代码取自https://stackoverflow.com/a/26741697/728750
<label ng-repeat="fruitName in ['apple', 'orange', 'pear', 'naartjie']">
<input
type="checkbox"
ng-model="fruitsGetterSetterGenerator(fruitName)"
ng-model-options="{ getterSetter: true }"
> {{fruitName}}
</label>
$scope.fruits = ['apple', 'pear']; // pre checked
$scope.fruitsGetterSetterGenerator = function(fruitName){
return function myGetterSetter(nowHasFruit){
if (nowHasFruit !== undefined){
// setter
fruitIndex = $scope.fruits.indexOf(fruit);
didHaveFruit = (fruitIndex !== -1);
mustAdd = (!didHaveFruit && nowHasFruit);
mustDel = (didHaveFruit && !nowHasFruit);
if (mustAdd){
$scope.fruits.push(fruit);
}
if (mustDel){
$scope.fruits.splice(fruitIndex, 1);
}
} else {
//getter
return $scope.user.fruits.indexOf(fruit) !== -1;
}
}
}
不理解上面的代码。
问题出在这里。见下面的代码$scope.fruitsGetterSetterGenerator = function(fruitName){
return function myGetterSetter(nowHasFruit){}
}
fruitsGetterSetterGenerator 是一个函数,它从第一行返回 myGetterSetter 中的另一个函数,这种方法或模式对我来说并不清楚。请帮我理解。感谢