gpt4 book ai didi

javascript - 在 angularjs 中,不同 Controller 中的选择选项 - 如何刷新?

转载 作者:行者123 更新时间:2023-11-30 17:47:25 24 4
gpt4 key购买 nike

有两种不同的 Controller ,一种用于语言,langCntl,另一种用于单词,wordCntl

wordCntl 记录中有一个名为 ln 的属性。 ln 使用 ng-select 显示在表单中,ngOptions 基于 langCntl Controller 中的记录。

langCntl 中的记录发生变化时,如何使 lnselectwordCntl 中刷新?

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>

(对我而言)什么有趣:

  1. langCntl.langs 更改后,wordCntl.langs 仍然是一个空数组。
  2. ngoptions中,有没有办法直接使用langCntl中的记录?

最佳答案

引用angualrjs sdk: $broadcast(name, args)

Dispatches an event name downwards to all child scopes (and their children) notifying the registered ng.$rootScope.Scope#$on listeners.

The event life cycle starts at the scope on which $broadcast was called. All listeners listening for name event on this scope get notified. Afterwards, the event propagates to all direct and indirect scopes of the current scope and calls all registered listeners along the way. The event cannot be canceled.

关于javascript - 在 angularjs 中,不同 Controller 中的选择选项 - 如何刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19855258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com