The AngularJS directive fails to display any content within its element children

Trying to retrieve the innerHTML of the selected node in options

The

<i class="before"></i>
tag should display text from either the selected or the first option when the page loads, within the same group. However, only dropdowns with hardcoded values display a value on load, while dropdowns generated through ngRepeat do not display anything.

var myapp = angular.module('myapp', []);
myapp.controller('myCtrl', function($scope) {
  
    $scope.investments = [
      {"name": "AARP Operating Funds"},
      {"name": "Some Big Title"},
      {"name": "I hatez IE8"},
      {"name": "I Love FF DeveLoper Edition"},
      {"name": "Give Me Some Sunshine"}
    ];

    $scope.investment = $scope.investments[0].name;


  })
  .directive('dropdownSelectBox', function(){


    return function(scope, element, attrs) {

      //.filter('option[value="' +  element.find('select').val() + '"]').text()

      console.log(element.find('select').children());

      if (element.find('select').val() == '') {
        element.find('i').text($('select').eq(0).text());
      } else {
        element.find('i').text(element.find('select').children()[0].innerHTML);
      }

      element.find('select').bind('change', function(){                    
          element.find('i').text($(this).find(':selected').text());
      });
      
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="myCtrl">

  <span class="dropdown" dropdown-select-box="">
      
      <i class='before'></i>
      <select id="timePeriod">
          <option value="7" selected="selected">Last 7 Days </option>
          <option value="30">Last 30 Days </option>
          <option value="60">Last 60 Days </option>
          <option value="current">Current Year </option>
          <option value="prior">Prior Year</option>
      </select>

    </span>


    <span class="dropdown" dropdown-select-box="">
      
      <i class='before'></i>
      <select ng-options="opt.name as opt.name for opt in investments | orderBy:'name'" ng-model="investment"></select>

    </span>

</div>

Answer №1

It is recommended to utilize ng-model:

var myapp = angular.module('myapp', []);
myapp.controller('myCtrl', function($scope) {
  
    $scope.investments = [
      {"name": "AARP Operating Funds"},
      {"name": "Some Big Title"},
      {"name": "I hatez IE8"},
      {"name": "I Love FF DeveLoper Edition"},
      {"name": "Give Me Some Sunshine"}
    ];

    $scope.investment = $scope.investments[0];
    $scope.period = 'Last 7 Days';

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="myCtrl">

  <span class="dropdown" dropdown-select-box="">
      
      <i class='before'>{{period}} {{investment.name}}</i>
      <select id="timePeriod" ng-model="period">
          <option value="Last 7 Days" selected="selected">Last 7 Days </option>
          <option value="Last 30 Days">Last 30 Days </option>
          <option value="Last 60 Days">Last 60 Days </option>
          <option value="Current Year">Current Year </option>
          <option value="Prior Year">Prior Year</option>
      </select>

    </span>


    <span class="dropdown" dropdown-select-box="">
      
      <i class='before'></i>
      <select ng-options="opt.name for opt in investments | orderBy:'name'" ng-model="investment"></select>

    </span>

</div>

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

Variables are losing their way in the vast expanse of self-submitting

I have a form that needs to be submitted on the same page (index.php) and I want to capture the entered information as a JavaScript variable. <?php $loginid = $_POST[username] . $_POST[password]; ?> Here is some basic code: <script> var pass ...

Is it possible to delay Vue rules from validating until a user interacts with an element?

For a project I am working on, I have implemented a v-date-picker where users can select a departure date and a return date. Interestingly, while most of the input field validations only occur after user interaction, the departure date picker displays an e ...

Is it possible to pass a mongoDB object to all prototype functions in JavaScript/Node.js?

I am currently facing challenges while working with NodeJS and MongoDB. I am struggling to pass the mongoDB object between prototype functions within my code. Can someone provide guidance on how to effectively pass this object between prototypes? In the m ...

Troubleshooting: AngularJS $uibModal Issue Not Resolved

Could you help me troubleshoot my code issue? I can see the initial HTML page, but clicking on "Open" does not trigger any action. There are no errors logged in the console or any other changes observed. app.js var app = angular.module('carApp' ...

What is the process for transferring items using ref ObjectId's?

https://i.sstatic.net/pi6js.pngI'm currently in the process of developing a small personal betting website. I have successfully created Arrays of ObjectId's within each collection that reference one another. However, I am facing challenges when i ...

Defining columns in jQuery DataTables for handling multiple data fields

Currently, I am utilizing jQuery DataTables with server-side processing for managing my list. However, I have encountered a roadblock in displaying multiple column data from the database within the same column header. How do I go about defining this speci ...

Oops! The file or directory you are looking for does not exist. Please check the location and try again

This is the content of my server.js file: var express = require('express'), app = express(); app .use(express.static('./public')) .get('*',function (req,res) { res.sendfile('/public/main.html' ...

Having trouble retrieving the complete URL on the Login Page within Shopify

I've been working on a feature where users are redirected to different URLs based on the page they click the login button on. For example, I have a registration page and a regular page, both with a login button. When a user clicks the login button on ...

Retrieving the width and height of a DIV element using Javascript

Currently, I am attempting to retrieve the width and height of a div element as it is being changed by the user and then pass that data to another page. However, I am encountering difficulties in obtaining the width and height values. <script> $(fun ...

Identify changes in attributes on elements that are updated automatically

On my webpage, I want to have two select boxes that are connected so their values always match. Here's an example: Imagine a page with two selects: <select class="myselector"> <option value='1'>1 <br> < ...

Glide your cursor over the button as it swirls with

I am attempting to create a button with an animation inside that includes both text and the animation itself. Currently, the animation (switching from png to gif) only works when I hover my mouse over a specific area of the button, rather than the entire b ...

I am on the lookout for a spell-checking tool that can seamlessly integrate with both Django and Python 2.7

Is there a way to detect spelling errors in real-time as the user types, with support for multiple languages? Your help would be greatly appreciated. Thanks! ...

How can the marionette controller object generate unique prototype methods that are dynamic?

Currently, I am working with Marionette to develop an application with multiple pages. The process of instantiating views and displaying them through the appRegion in each controller/router method feels repetitive. My goal is to streamline this process by ...

Display a div using JavaScript when the mouse moves and make it follow the cursor

In my application, I have a set of customer records displayed as rows in a table. I am looking to implement a feature where, upon hovering over a record (row), a div will pop up showing more detailed information about that specific record. This hover-over ...

Iterate over an array containing multiple objects, search for a specific parameter, and append any matching object to a fresh array

Having just started learning JS and React, I've hit a roadblock. Despite scouring through Stack Overflow for answers, I have yet to find a solution with a clear explanation that fits my needs. Let me share the arrays causing my confusion. The first ...

"Div does not perfectly match the height of its parent div when using the 'inherit' value

I have 3 divs inside a parent div, intended to be arranged like so: ______________________________________ | | | HEADER | |______________________________________| ____ ________________ ...

VS code showing live server as installed but failing to function properly

After installing the live server extension, I noticed that my browser is not updating when I save my HTML or other files. What could be causing this issue? ...

Developing a unique lattice structure that is enclosed within a specific form

I'm facing a challenge with creating grids in Canvas that are confined within a specific shape. While I can easily create a grid to fit a square, the task becomes trickier when dealing with irregular shapes. Here's my current progress: I aim to l ...

Extract information from a database using AngularJS for the purpose of creating visual charts and graphs

After conducting extensive research and seeking assistance from various sources, I came across a helpful guide at . While this resource was close to what I needed, I encountered difficulties when attempting to integrate live database functionality with my ...

How can I use Ajax code to send data to a PHP page and receive the results as a JSON-encoded multidimensional array containing information on each item?

Apologies for the unconventional question title. What I am trying to accomplish is managing two tables in my database: a hotel table (table1) and a room type table (table2). My goal is to allow customers to customize their travel packages by changing hote ...