Angular's created by directive will only receive updates and not update any other components

My directive is not updating the <select> element itself when the ng-model scope variable is changed, but it does update when I change the scope variable bound to it. I believe I may need to register a $watch in the link function, but I'm uncertain. Any assistance would be appreciated.

template.html

<div ng-repeat="searchField in searchFields">
    <select-search-field label="{{searchField.label}}" field="$parent[searchField.field]"></select-search-field>
</div>

resulting html

<div>
    <label>Landing Gear</label><br>
    <select ng-model="gear">
        <option value="">Any</option>
        <option value="fixed">Fixed</option>
        <option value="retractable">Retractable</option>
    </select>
</div>

controller.js

$scope.searchFields = [{
    label: 'Landing Gear',
    field: 'gear'
}];

$scope.gear = ''; //This value is passed to the directive in the "field" property ($parent[] part)

directive.js

.directive('selectSearchField',function() {
  function template() {
    return '<label>{{label}}</label><br>' + 
      '<select ng-model="field">' + 
      '<option value="">Any</option>' + 
      '<option value="fixed">Fixed</option>' + 
      '<option value="retractable">Retractable</option>' + 
      '</select>';
  }

  return {
    restrict: 'E',
    scope: {
      label: '@',
      field: '=',
      options: '='
    },
    template: template()
  };
});

Edit: Here are the files on github: https://github.com/andypandy/littleplanefinder (refer to public/js/controllers.js and public/js/directives.js)

Also: view it live at

Answer №1

Seems like you're displaying the label with {{label}}, but not updating the field, resulting in no updates since the label is never refreshed.

No need for the $parent syntax with the field attribute; it will be evaluated within the isolated scope.

I created a demo on JSFiddle that demonstrates what you're trying to achieve http://jsfiddle.net/G5UCm/3/.

You can click the "get label" button to see the value the controller recognizes, confirming that two-way data-binding is correctly set up.

Key parts are highlighted below:

// Inside the controller's scope in the markup
<select-search-field field="searchField.field">
</select-search-field>
// Within the template, remember to show the item that is being modified!
return '<label>{{field}}</label><br>' + 

Edit: Adjustment based on new information

It appears you're attempting to use ng-model on the parent gear property. This is a bit unconventional as you're trying to bypass the isolation. You could simply avoid isolation to inherit scope variables. Alternatively, keep passing the field as a two-way data binding and utilize the model on the parent, like so:

'<select ng-model="$parent[field]">' + 

Updated demo: http://jsfiddle.net/G5UCm/4/

Hopefully, this clarifies things for you!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Boost the elements' worth within an array

Assume I am working with an array shown below... let myArr = [0,0,2,0,0]; I am aiming to generate a ripple effect where the modified array becomes [0,1,2,1,0] ...

Prevent postback in case of validation error

In my current setup, I have a textbox control with the following specifications: <asp:TextBox ID="txtAmount" runat="server" AutoPostBack="true" Width="85px" class="validate[ERExpenseTypeDaterequired,custom[float]]" On ...

Having trouble importing the module I developed in Node

I've been struggling to understand why this import is failing. Despite trying numerous approaches, modifying the export and import based on various tutorials online, it continues to result in failure every time. This should be a simple task in theory. ...

Struggling to properly line up the baselines of navigation list items that are styled as circular elements using CSS

I have transformed my navigation menu into a series of CSS circles with text inside. The issue I am facing is that the text spills out unevenly based on the amount of content in each circle. To address this, I used a JavaScript solution to center-align the ...

What is the best way to transfer information between different pages in an HTML document?

I am facing a specific requirement where I must transfer form data from one HTML page to another HTML page. The process involves a total of 5 pages, with the user entering data in the following order: 1st page: Name 2nd page: Weight 3rd page: Height 4t ...

Transform a checkbox input into two distinct buttons

I am trying to change the input checkbox that toggles between light and dark mode into two separate buttons. How can I achieve this functionality? Check out the demo here: https://jsfiddle.net/ot1ecnxz/1 Here is the HTML code: <input type="checkb ...

A cautionary alert is triggered by vsCode when analyzing seemingly correct code sourced from vue.js documentation

While using Visual Studio Code version 1.13.1V and referring to the vue.js guide on lazy loading, I encountered an issue when writing the following code snippet: import Vue from 'vue' import Router from 'vue-router' const Health = () = ...

Angular 6 - Outdated Functions

I'm having trouble updating the request options as they are now deprecated. I can't seem to locate the alternative option for this. Can anyone offer some assistance? import { Injectable } from '@angular/core'; import { HttpClient } fr ...

Angular version 1.5 now supports components with default values for @ bindings

Is there a way to set a default value for an @ binding of a component? I found instructions on setting default values in Angular Directive Scope at this link. However, components do not support the compile function. Here is an example of a component: { ...

Sorting an array of strings in JavaScript with the help of another array of strings

I am looking for a way to rearrange array1 based on array2. var array1 = ["cat","dog","mouse","elephant","ant","cow","goat","hen"]; var array2 = ["mouse","cow","goat"]; Expected output: var another_array = ["mouse","cow","goat", "bird"--- then rest of ...

Navigating to a new page once a backend function in Express has finished executing

Recently, I have been experimenting with express web servers to create a website that allows users to sign in using Discord's OAuth2 API. In order to secure sensitive information, I have been utilizing the express-session npm module to store data with ...

What is the best way to preserve an apostrophe within a variable in JavaScript without it being replaced?

How can I send the value of NewText in its original form from .cs code using an ajax call? **var NewText ="D'souza";** $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: " ...

The box shadow feature seems to be malfunctioning on Android devices

It seems that the box shadow is not functioning properly on Android devices, without any clear explanation as to why. My attempt to address this issue included the following code: -webkit-box-shadow: inset 0 0 0 2px #F3D280; box-shadow: inset 0 0 0 2px # ...

Removing data with the click of a button

I have successfully implemented a feature where clicking the "add to my stay" button displays the name and price data. Subsequently, it automatically changes to a remove button when clicked again for another addon. If I press the remove button of the first ...

Node.js Axios Returns Bad Request with Status Code 400

I am currently facing an issue while trying to send the results of a nodejs query to an endpoint. Interestingly, I receive a successful response when using Postman with the correct parameters, but encounter errors when attempting to use axios. data: ' ...

Testing the functionality of an Express Rest API with Mocha unit tests

I have just started diving into the world of unit testing. While I've been successful in running simple tests such as "adding two numbers and checking if the result is greater than 0", my goal now is to develop a REST API using Test-Driven Development ...

What is the method for receiving socket emits in sails.io.js?

I am currently utilizing Sails 0.11 for the back-end and angularjs for the front-end of my project. Within Sails, I have a TwitterController containing the following code snippet to establish a connection with the Twitter Streaming API using the node modu ...

Dynamic Positioning in Javascript Application

When working on a javascript project, I needed to create a div element. Here is the code snippet I used: const divStyle = {left: offsetPercent + '%', 'position': 'absolute'}; <div style={divStyle}/> However, I encounte ...

Positioning a div at the bottom of a webpage without using absolute positioning in HTML

Check out the fiddle. I've included some CSS to position the tab on the right side. My goal was to have those tabs in the lower right corner of the container. If I use absolute positioning for the div, it messes up the tab content width. I tried ad ...

The issue with Angular 1.6 not displaying the scope value in the template

Hey there! I'm currently working on index.html Here's the code snippet from before: <body ng-app="MainController"> <div class="page page-base {{ pageClass }}" ng-view> </div> </div> Then, I made changes by ass ...