angularjsangularjs-directiveangularjs-model

ng-model does not work in select directive


What is working: The ng-options contains all names and it is shown in the dropdown-list.

Problem:

  1. At the beginning if nothing is selected, there should the text "Select name..." displayed. But it's empty right now.
  2. I know it has been asked a lot but I didn't found a solution with this case. If I will select some of those options the Error

"Error: [$compile:nonassign] Expression 'undefined' used with directive 'fileWidget' is non-assignable!" occured.

GeometryService.js

angular.module('COMLEAMclient')
.factory('GeometryService', function($resource) {

  var selectedGeometry = {name:'Select name...'};
}

FileWidgetDirective.js

angular.module('COMLEAMclient')
.directive('fileWidget', ['$log', '$parse', function($log, $parse) {

        return {
            scope: {
                data: '=',
                selectedFile: '=',
                updateSelection: '&',
                selected: '=',
                onReadFile: '&',
            },
           ....
        }
    )
}

InputController.js

<div file-widget data="iCtrl.geometryFiles"  update-selection= "iCtrl.setSelectedGeometry(name)" selectedFile= "iCtrl.selectedGeometryFile" on-read-file="iCtrl.saveGeometryFiles(file,name)"></div>

FileWidget.html

<select class="form-control"
    ng-options="elem.name for elem in data"
    ng-model="selectedFile"
    ng-change="onSelectionChange()">
</select>

Any ideas?


Solution

  • In order to have Select name... you need to have an option element:

    <select class="form-control"
        ng-options="elem.name for elem in data"
        ng-model="selectedFile"
        ng-change="onSelectionChange()">
        <option value="">Select name...</option>
    </select>
    

    You are trying to assign to an object, and that is non assignable, which is why you're getting the non-assignable error. You need to have a variable that can contain the value the selected option, try:

    ng-model="selectedFile.name"