I have a custom directive in angularjs. Basically, I want the user to select a value from the selection field and add the value to the array. This leads to the call of my custom directive and the display of a new element on the screen. I want a text field to be generated to bind to a controller attribute.
HTML
<device-list ng-repeat="device in devices" key="device.key" display-name="device.display_name" bind-prefix="descriptions"></device-list>
Directive
angular.module('device_list_tag', []). directive('deviceList', function() { return { restrict: 'E', require: '?ngModel', scope: { devices: '=', key: '=', displayName: '=', bindPrefix: '@' }, link: function(scope, element, attrs) { var deviceListElement = $(element) var containerDiv = $('<div>') .addClass('row') var labelTag = $('<label>').text(scope.displayName) .addClass('span1') var bindField = attrs.bindPrefix+'.'+scope.key var textField = $('<input>') .addClass('span3') .attr('ng-model', bindField) containerDiv.append(labelTag) containerDiv.append(textField) deviceListElement.append(containerDiv) } } })
controller
function DevicesCtrl($scope) { descriptions = {} }
It seems that since ng-model is local to the scope of the directive, how can I apply it to the parent? If I have a bunch of text fields on a page like
<input ng-model="descriptions.test"/>
It works only with the exception of the fields created in the selection window.
source share