Sachin
Sachin

Reputation: 2361

Error: ngModel: Non-Assignable Expression

I have code which generates drop down list dynamically. I want to set selected value using ng-repeat, to do this i am using function in ng-model but i am getting

Error: [ngModel:nonassign] Expression 'item.blockName = getCurrentComboBoxValue(item.blockName.id, blockNameOptions)' is non-assignable. Element: <select class="form-control input-sm" ng-model="item.blockName = getCurrentComboBoxValue(item.blockName.id, blockNameOptions)" name="blockName" ng-options="choice.id as choice.value for choice in blockNameOptions">

HTML

<div ng-repeat="item in modulesData.blocks track by item.id">
    <select class="form-control input-sm" ng-model="item.blockName = getCurrentComboBoxValue(item.blockName.id, blockNameOptions)" name="blockName" 
        ng-options="choice.id as choice.value for choice in blockNameOptions">

    </select>
</div>

Controller

$scope.getCurrentComboBoxValue = function (id, availableData) {
    var result = _.where(availableData, { 'id': id });
    return result[0];
}

Upvotes: 0

Views: 5767

Answers (2)

Nix
Nix

Reputation: 58522

I think you are trying to do what angular does for you. If you give angular a "name" of a property to assign the selected value to it will automatically bind the value. when it changes. In your case the selectedValue would be choice.id. This value can either be an existing thing on scope, or it will assign it to scope. So for example:

<div ng-repeat="item in modulesData.blocks track by item.id">
    <select class="form-control input-sm" ng-model="selectedItem" name="blockName" 
    ng-options="choice.id as choice.value for choice in blockNameOptions">

    </select>
    <div>I selected  {{selectedItem}}</div>
</div>

and in your Ctrl you could do

$scope.getCurrentComboBoxValue = function (id, availableData) {
    console.log($scope.selectedItem);
}

If you would have defaulted $scope.selectedBlock = , it would have defaulted the select for you.

Upvotes: 0

Jeff Hubbard
Jeff Hubbard

Reputation: 9892

getCurrentComboBoxValue(blockNameOptions) is not valid on the left-hand side of an =. ngModel requires that the thing on the left-hand side is assignable.

Upvotes: 1

Related Questions