Leveraging various techniques within a single controller in AngularJS

I am seeking assistance and advice on a coding issue I am facing. I am attempting to use two methods in one controller. The first method is to display selected rooms, while the second method is to display selected pax. However, only the first method seems to be working as intended, as the second md-select does not display the array in the second method. Below are snippets of my JavaScript and HTML code:

var app = angular.module('CoreWebApp', ['ngMaterial', 'ngMessages', 'ngAnimate']);

app.controller('SelectedTextController', function($scope) {
  $scope.rooms = [1, 2, 3, 4, 5, 6, 7];
  $scope.selectedRoom;
  $scope.getSelectedText = function() {
    if ($scope.selectedRoom !== undefined) {
      return $scope.selectedRoom + " Room(s)";
    } else {
      return "Rooms";
    }
  };

  $scope.paxes = [1, 2, 3, 4, 5];
  $scope.selectedPax;
  $scope.getSelectedPersons = function() {
    if ($scope.selectedPax !== undefined) {
      return $scope.selectedPax;
    } else {
      return "Pax";
    }
  };
});
<div>
  <label>Rooms</label>
  <div layout-sm="column" layout-align="center end">
    <md-select md-no-resize ng-model="selectedRoom" md-selected-text="getSelectedText()">
      <md-optgroup label="rooms">
        <md-option ng-value="room" ng-repeat="room in rooms">{{room}} Rooms</md-option>
      </md-optgroup>
    </md-select>
  </div>
</div>

<div>
  <label>Pax</label>
  <div>
    <md-select ng-model="selectedPax" md-selected-text="getSelectedPersons()" aria-label="">
      <md-optgroup label="pax">
        <md-option ng-value="pax" ng-repeat="pax in paxes">{{pax}}</md-option>
      </md-optgroup>
    </md-select>
  </div>
</div>

Answer №1

Unfortunately, I am unable to execute your code for testing purposes, however, I believe the correction should be made as follows:

<md-optgroup label="pax">

needs to be updated to

<md-optgroup label="paxes">

Answer №2

If you're looking for some guidance on setting up your Angular app and controller, here is an example code snippet to get you started:

var app = angular.module('CoreWebApp', []);

app.controller('SelectedTextController', function($scope) {
  $scope.rooms = [1, 2, 3, 4, 5, 6, 7];
  $scope.selectedRoom;
  
  $scope.getSelectedText = function() {
    if ($scope.selectedRoom !== undefined) {
      return $scope.selectedRoom + " Room(s)";
    } else {
      return "Rooms";
    }
  };

  $scope.paxes = [1, 2, 3, 4, 5];
  $scope.selectedPax;
  
  $scope.getSelectedPersons = function() {
    if ($scope.selectedPax !== undefined) {
      return $scope.selectedPax;
    } else {
      return "Pax";
    }
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="CoreWebApp" ng-controller="SelectedTextController">

<div >
  <label>Rooms</label>
  <div layout-sm="column" layout-align="center end">
    <md-select md-no-resize ng-model="selectedRoom" md-selected-text="getSelectedText()">
      <md-optgroup label="rooms">
        <md-option ng-value="room" ng-repeat="room in rooms">{{room}} Rooms</md-option>
      </md-optgroup>
    </md-select>
  </div>
</div>

<div>
  <label>Pax</label>
  <div>
    <md-select ng-model="selectedPax" md-selected-text="getSelectedPersons()" aria-label="">
      <md-optgroup label="pax">
        <md-option ng-value="pax" ng-repeat="pax in paxes">{{pax}}</md-option>
      </md-optgroup>
    </md-select>
  </div>
</div>
  
  </div>

Answer №3

It seems like your code is in good shape, but without seeing the full code it's hard to say for sure. Just double-check that you have included the ng-controller and ng-app attributes in your HTML.

Below is an example of how your code should look with ng-controller and ng-app included. Also, make sure you have all the necessary JavaScript and CSS libraries linked.

Check out this Plunker for a demonstration

<body ng-app="CoreWebApp" ng-controller="SelectedTextController">
<div>
  <label>Rooms</label>
  <div layout-sm="column" layout-align="center end">
    <md-select md-no-resize="" ng-model="selectedRoom" md-selected-text="getSelectedText()">
      <md-optgroup label="rooms">
        <md-option ng-value="room" ng-repeat="room in rooms">{{room}} Rooms</md-option>
      </md-optgroup>
    </md-select>
  </div>
</div>
<div>
  <label>Pax</label>
  <div>
    <md-select ng-model="selectedPax" md-selected-text="getSelectedPersons()" aria-label="">
      <md-optgroup label="pax">
        <md-option ng-value="pax" ng-repeat="pax in paxes">{{pax}}</md-option>
      </md-optgroup>
    </md-select>
  </div>
</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

Microsoft Edge browser incorrectly calculates range.endOffset value

This particular problem is specific to the Microsoft Edge browser. I am attempting to apply a CSS style to a selected word using Range API's, but I am encountering an issue with the range.endOffset functionality in Edge. Below is the code snippet I am ...

What is the process of utilizing an npm package as a plain JavaScript library through require and module export?

Hey there, I'm a bit unsure if I'm on the right track with my question, but here it goes: What steps do I need to take to modify a node.js package for use as a standalone embedded script/library in HTML? How can I invoke a class constructor in ...

Mastering preventBack() Functionality - A Foolproof Method to Eliminate a Div on Back

This unique code prevents the page from going back when using the back button: <script type = "text/javascript" > function stopGoingBack(){window.history.forward();} setTimeout("stopGoingBack()", 0); window.onunload=function(){null}; ...

Customize ion-icon within a Tab button using ng-class in Ionic AngularJS

I'm struggling to update the icon on Tab from "ion-social-google-outline" to "ion-social-google" once it's clicked, but I haven't been able to crack the code... Because I need to navigate within an ion-slide-box, I can't utilize ion-ta ...

How to utilize FileReader for parsing a JSON document?

I'm currently facing an issue while attempting to read and copy a JSON file uploaded by the user into an array. When using .readAsText(), the returned data includes string formatting elements like \" and \n. Is there a way to utilize FileRe ...

What is the sequence in which services, factories, and providers are executed in AngularJS?

Is the execution order of all the mentioned tasks in AngularJS framework predefined, or is it left up to the programmer to determine? ...

Sending data from a parent scope to a directive's controller

I have a directive in my code that has a separate controller in a JavaScript file. This controller has a scope variable called parameterDatabase which needs to be populated from the calling page. I am struggling to find a way to pass a value to it. <bo ...

Leveraging TypeScript to Access Parameters in React Router

Currently, I am delving into the realm of TypeScript usage in my React projects and I have encountered a stumbling block when it comes to implementing React Router's useParams() feature. My import statement looks like this: import { useParams } from ...

Retrieve the values from multiple columns within a jqgrid row

Is it possible to retrieve both the first column value and the second column value from a jqgrid row? In my other program, I am currently using this code to obtain the first value of the row: $("#tblTallySheet").jqGrid('getGridParam', 'selr ...

Distinguishing CSS Variances on ASP.Net Site with Android 4+

I am encountering an issue with my current ASP.Net Mobile Website design. It is rendering correctly on android phones pre 4, iPhones, and Blackberries. This problem requires a bit of setup to explain... CSS tr { color: #00ff00; } #MasterBackButton { ...

Discover the secret to smoothly scrolling to an element in ReactJs

Currently, I am in the process of constructing a Single Page Application (SPA) using React and one key functionality I need to implement is navigation that scrolls to specific sections on the page when clicked. This behavior is similar to how anchor tags w ...

Tips on redirecting the treeview menu using Material-UI 4

Currently, I am utilizing Material UI 4's Treeview component. However, I am struggling to figure out how to include a parameter that allows me to redirect to other pages when the user clicks on an item. In the past, I relied on a different method. Ch ...

Unfortunately, the datepicker feature does not seem to work properly on elements that have been dynamically added through the

My code progress so far is as follows: $('body').on('change', '.dropdown', function() { $.ajax({ success: function(res) { if(res.success == true) { return elements that have the class ...

How can the onclick attribute be modified in an HTML document?

I am looking to update the data-pro-bar-percent attribute of a progress bar from 80 to 100 when a specific link is clicked. The change in attribute needs to be as follows: data-pro-bar-percent="80" --> data-pro-bar-percent="100" This is the current HT ...

How can you extract path information from an SVG file and utilize it as a marker on Google Maps?

I want to implement a custom SVG icon as a marker on Google Maps. I have obtained this SVG file: <svg xmlns="http://www.w3.org/2000/svg" width="510px" height="510px" viewBox="0 0 510 510"> <g stroke="black" stroke-width="10" ...

Absence of receiving any HTTP error codes when making REST calls

In our application, it is crucial to differentiate between 400 and 500 range error codes for distinct processes. Let's consider three REST calls: The first returns a status code of 200, the second returns 401, and the third returns 502 Initially, ...

Steer clear of duplicating patterns in vue templates

I have a significant issue with a component that needs to be repeated multiple times in the parent template due to the usage of v-if. The component code is as follows: <SelectCard v-for="(channel, index) in category.visibleChannels" :key="index + & ...

I'm having trouble understanding why my Javascript validation suddenly stopped functioning. Can anyone assist me in troubleshooting this issue?

I have been working on this webpage for a school project for a few days, and it was running smoothly until about 10 minutes ago. The only change I made was adding an extra JavaScript validation. Now, when I try to register by clicking the "register" butt ...

Displaying JavaScript - Nothing to Echo in PHP

Using PHP to echo a JavaScript block, I have encountered an error message. echo "<script language='javascript' type='text/javascript'> jQuery(document).ready(function($){ var $lg = $('#mydiv'); ...

Why won't JavaScript functions within the same file recognize each other?

So I have multiple functions defined in scriptA.js: exports.performAction = async (a, b, c) => { return Promise.all(a.map(item => performAnotherAction(item, b, c) ) ) } exports.performAnotherAction = async (item, b, c) => { console.log(`$ ...