这是我的HTML:
<form method="post">
<div class="form-group" data-ng-switch="name">
<div data-ng-switch-when="null">
<label>Name</label>
<input class="form-control" name="name" id="name" data-ng-model="vendor.name" >
</div>
<div data-ng-switch-default>
<label>Name: </label>
<label>{{vendor.name}}</label>
<input type="hidden" class="form-control" name="name" id="name" data-ng-model="vendor.name" >
</div>
</div>
<div class="form-group">
<label>Description</label>
<input class="form-control" name="description" id="description" data-ng-model="vendor.description" >
</div>
<br>
<div data-ng-switch="name">
<button type="button" class="btn btn-primary pull-right" data-ng-switch-when="null" data-ng-click="save()">Save</button>
<button type="button" class="btn btn-primary pull-right" data-ng-switch-default data-ng-click="update()">Save</button>
</div>
</form>
这是我的控制器的一部分:
$scope.save = function() {
// vendor.name does not exist in here, why?
Vendor.get($scope.vendor.name).
success(function(response) {}).error(function() {
var postData = {
'name': $scope.vendor.name,
'description': $scope.vendor.description
};
Vendor.update(postData);
});
};
我检查了正在调用save()。为什么 vendor.description 确实存在,但在调用Vendor.get函数之前 vendor.name 不存在?