Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing ng-model within a directive
    primarykey
    data
    text
    <p>I've got a custom defined directive in angularjs. Basically what I want to happen is the user will pick a value from a select box and that will append a value to an array. This causes my custom directive to be invoked and render a new element on the screen. I want the text field that the directive generates to bind to the controller's attribute.</p> <p>Html</p> <pre class="lang-html prettyprint-override"><code>&lt;device-list ng-repeat="device in devices" key="device.key" display-name="device.display_name" bind-prefix="descriptions"&gt;&lt;/device-list&gt; </code></pre> <p>Directive</p> <pre class="lang-js prettyprint-override"><code>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 = $('&lt;div&gt;') .addClass('row') var labelTag = $('&lt;label&gt;').text(scope.displayName) .addClass('span1') var bindField = attrs.bindPrefix+'.'+scope.key var textField = $('&lt;input&gt;') .addClass('span3') .attr('ng-model', bindField) containerDiv.append(labelTag) containerDiv.append(textField) deviceListElement.append(containerDiv) } } }) </code></pre> <p>Controller</p> <pre class="lang-js prettyprint-override"><code>function DevicesCtrl($scope) { descriptions = {} } </code></pre> <p>It appears as though as ng-model is local to the directive's scope, how do I make it apply to the parent? If I have a bunch of text fields on the page like</p> <pre class="lang-html prettyprint-override"><code>&lt;input ng-model="descriptions.test"/&gt; </code></pre> <p>It works works except with the fields generated by the select box.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload