Photovor
Photovor

Reputation: 403

Update ng-model from controller

Hopefully this is pretty simple, but my brain just isn't working at the moment...

I have a form like this:

<div class="control-group">
    <label class="control-label" for="CN_PREF_NAME_J04">Name on Card:</label>
        <div class="controls">
            <input type="text" name="CN_PREF_NAME_J04" id="CN_PREF_NAME_J04" ng-model="formData.CN_PREF_NAME_J04"  />
        </div>
</div>

and I have a basic controller like this:

app.controller("CloseCallSpoke", function($scope){
    $scope.formData = angular.copy($scope.data);
}

This will automatically assign the entire formData object whatever is in my data object.

How can I just assign CN_PREF_NAME_J04?

i tried:

$scope.formData.CN_PREF_NAME_J04 = angular.copy($scope.data.CN_PREF_NAME_J04);

but I get "$scope.formData is undefined.

Please help.

Upvotes: 0

Views: 117

Answers (1)

jandersen
jandersen

Reputation: 3561

try this:

$scope.formData = { CN_PREF_NAME_J04 : angular.copy($scope.data.CN_PREF_NAME_J04) };

That way $scope.formData is initialized with an object that has the CN_PREF_NAME_J04 property you're looking for

Upvotes: 1

Related Questions