To make the answer provided by @abourget more complete, the value of the scopeValue [field] field in the next line of code can be undefined. This will result in an error when setting up the subfield:
<textarea ng-model="scopeValue[field][subfield]"></textarea>
One way to solve this problem is to add the ng-focus = "nullSafe (field)" attribute, so your code will look like this:
<textarea ng-focus="nullSafe(field)" ng-model="scopeValue[field][subfield]"></textarea>
Then you define a nullSafe (field) in the controller, as shown below:
$scope.nullSafe = function ( field ) { if ( !$scope.scopeValue[field] ) { $scope.scopeValue[field] = {}; } };
This ensures that scopeValue [field] is not undefined before setting any value to scopeValue [field] [subfield].
Note: you cannot use ng-change = "nullSafe (field)" to achieve the same result, because ng-change occurs after changing the ng model, which will cause an error if scopeValue [field] is undefined.
Max Jun 28 '14 at 23:40 2014-06-28 23:40
source share