Is it possible to refresh the options for a select in AngularJS when they are in different controllers?

Two separate controllers are being used here; one for language, known as `langCntl`, and one for words, referred to as `wordCntl`.

Within the `wordCntl` controller, there is an attribute called `ln`. This attribute is displayed in a form using `ng-select`, with `ngOptions` populated based on records in the `langCntl` controller.

The question at hand is how to update the `select` field for `ln` in the `wordCntl` when the records in `langCntl` change.

langMod.js

var langMod = angular.module('langMod', ['ngResource']);

langMod.controller('langCntl',function($scope,$http) {
  $scope.langs = [];
  $scope.reset = function() {
    $http.get( '/lang.jsn').success( function(data) {
      console.log( 'http.success: data='+data );
      $scope.langs = angular.copy($scope.origs);
    });
  };
  $scope.reset();
});

langMod.controller('wordCntl',function($scope) {
  var langElem = document.querySelector("[ng-controller='langCntl']");
  $scope.langs = angular.element(langElem)).scope().langs;
  $scope.rcd = { ln: 'en', word: '?' };
});

index.html

<html lang='en' ng-app='langMod'>
<body>
  <ng-form ngForm='abc' ng-controller='wordCntl'>
    <select ng-model="rcd.ln" ng-options="c.lang as c.name for c in langs">
  </ng-form>
</body>
</html>

Some intriguing observations:

  1. Despite changes in `langCntl.langs`, `wordCntl.langs` remains empty.
  2. In the `ngoptions`, is it possible to directly use the records from `langCntl`?

Answer №1

Check out the AngularJS SDK documentation: $broadcast(name, args)

This function sends an event named 'name' down to all child scopes, notifying any registered ng.$rootScope.Scope#$on listeners.

The event begins at the scope where $broadcast was called. All listeners looking for the 'name' event on this scope are informed. The event then moves through all direct and indirect scopes of the current scope, calling all registered listeners along the way. It is important to note that the event cannot be stopped or canceled.

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

Execute asynchronous code in a Next.js component without relying on the UseEffect hook

Within my nextjs application, there is a StrapiImage component that takes in an image object from the strapi backend api as a prop. This component sets the width, height, URL, and any additional props for the image. Essentially, it serves as a shortcut for ...

Utilize interpolation with ES6 and an Angular 1.4 directive

Currently experimenting with the unique ES6 + Angular combination and facing a challenge in interpolating an html string within a directive that includes scope bindings. We have attempted the following approach: Current scenario The code below is functi ...

Why React's setState is only returning the last item when a list is saved to a variable

Being a newcomer to React, I am currently working on a small project that involves using the GitHub API to perform a search and display the results on the screen via AJAX. Right now, I am utilizing a for loop to iterate through the response data, saving it ...

Obtain the value of an element from a React UI Material component (e.g. ListItemText)

Incorporated an onClick() event where a function is invoked to retrieve and display the value of any clicked element within the HTML body in a web component. <div id="root" onclick="handleClick(event)"></div> This snippet o ...

There seems to be a glitch with jQuery on my Angular.js website

I'm trying to implement Masonry.js on my website, and although I've managed to make it work, the solution feels like a messy workaround and I can't quite figure out why it's functioning (and not functioning well). The primary issues I& ...

Encountered an error in Angular1: TypeError - promise.catch does not exist as a

Upon using angular-ui-router, I encountered the following error while clicking on the links view1 and view2 in index.html. The same example worked with the regular angular router. Is there something missing in the code? Thanks. TypeError: promise.catch i ...

Sending the user to their destination

There is a code in place that triggers when an email is sent to the user, containing a link that leads them to a specific endpoint upon opening. I am experiencing issues with redirection at the end of the function. The redirect does not seem to be working ...

Having trouble with jQuery.validate.js while using type="button" for AJAX calls in an MVC application

I have come across several questions similar to mine, but I haven't found a solution that fits my situation in MVC, so I am reaching out for help. I am a beginner with MVC and I am utilizing jQuery AJAX to invoke a controller method for data insertio ...

Steps for extracting URL parameters from AWS API Gateway and passing them to a lambda function

After successfully setting up my API gateway and connecting it to my lambda function, I specified the URL as {id} with the intention of passing this parameter into the lambda. Despite numerous attempts using both the default template and a custom one for ...

Reset the checked state in React JSX to false by using a reset button

After attempting to reset selected radio buttons on this list, it seems like the change I made from {checked} to {user.checked} in input check is causing issues. This modification can be traced back to UserListElement.tsx In an effort to resolve this issu ...

Scrolling background for a nested Bootstrap modal

I am facing an issue with a modal that has a lengthy content and a button to open another modal. Once the inner modal is closed, the outer modal becomes unresponsive. It fails to scroll and instead, the background starts to scroll. While I have come acros ...

Utilize UI-Router $stateProvider in Angular run block for Promise Resolution

UI-Router has different capabilities compared to Angular's ngRoute. It not only supports all the features of ngRoute but also provides additional functionalities. I am transitioning my Angular application from ngRoute to UI-Router. However, I'm ...

Discover the steps to linking a dropdown menu to a text input using HTML and JavaScript

I'm currently using Sublime and trying to link a dropdown menu with an input text using html, css, and javascript. When the user selects an option from the dropdown menu, I want the corresponding value to appear in the text input field. For example, i ...

Is there an issue with loading CSS and JavaScript in mobile view on a website built with CodeIgniter?

My experience with codeigniter has been great so far, but I encountered an issue when trying to view my work on different devices. Here is the problem: Web: Broswer View | Browser (Responsive Design View) Mobile: Screenshot 1 | Screenshot 2 _htaccess: ...

AngularJS directive doesn't refresh template when scope values are fetched dynamically through ajax requests

Attempting to give this question a precise title as possible, I find myself struggling with an issue in AngularJS. While trying to showcase my problem through a jsfiddle, it turned out to be too reliant on multiple files and not yet accessible online. So p ...

Ways to retrieve JSON string from responsetext in JavaScript

After spending the entire day on this, I still can't figure it out. I have AJAX that fetches a JSON string from a PHP script, but now I need to work with it in JavaScript. Here's what I've tried: var xmlhttp; xmlhttp=new XMLHttpRequest(); ...

Update Text in B-table Cell using Boostrap Vue

I need to change the text color of each cell in a column within a b-table. I have successfully changed the text color of the column header, but what about the individual cell texts? Here is the current b-table code: <b-card title="Total"> ...

Utilizing the power of jQuery's $.each method to dynamically generate HTML select options within an AJAX

I am working with a bootstrap modal that includes a form which requires data from the database. To retrieve this data, I am using PHP as shown below: public function get_view_for_inspection_report_belum_eor(){ $q = $this->inspection->get_view_fo ...

Unable to programmatically trigger a login button click or form submission in Swift using WKWebView

Issue: Unable to submit webview form for logging into a public website using app code. Background: Successful injection of username/email and password into respective form fields, but unable to click the button or submit the form through the app code. Var ...

What other ways can websockets be utilized besides comet?

Websockets offer a more efficient solution for comet (reverse Ajax, often achieved through long-polling). However, are there other ways we can utilize websockets? For instance: - Can websockets be used to facilitate communication between different bro ...