7

In my application I have a model attached to a form which is something like that:

$scope.location = { description: "my descriptive description", address: "blah" }

Cleaning the field "description" in the form, which is bound to ng-model="location.description", removes the field from $scope.location which becomes:

$scope.location = { address: "blah" }

Now I would like it to retain the "description" field. What can I do in order to achieve this behaviour?

Thanks for your help

4

1 回答 1

0

一种可能性是使用 ng-change 指令:

<input ng-model="desc" ng-change="setDescription()">

在你的控制器中:

$scope.setDescription = function(){
    $scope.location.description = $scope.desc ? $scope.desc: "default" 
}

基于 Kozlowski 的评论:http: //jsfiddle.net/myMyQ/2/

于 2012-09-16T11:17:09.373 回答