RudziankoŇ≠ RudziankoŇ≠ - 29 days ago 13
AngularJS Question

AngularJS: TypeError: Cannot set property of undefined

Probably I completely don't understand Angular. My question is:

On UI I have following:

<input ng-model="$ctrl.newItem.action" id="action" name="action" class="form-control"/>


So
action
element is two-ways binded to angular model.

Now in the other part of UI but with the same controller I call edit method:

$ctrl.updateItem = function (item) {
$ctrl.newItem.action = item.action;
//...


I would like
$ctrl.newItem.action
show action of currently being edited element bu instead i see error in console:

TypeError: Cannot set property of undefined


Why
$ctrl.newItem.action
is undefined?

Answer

angular models undefined until you define it in controller or type something in input

try this one

$ctrl.updateItem = function (item) {
            $ctrl.newItem = {action: item.action};
            //...