以编程方式更改模型时,选择框不会更新

问题描述 投票:0回答:2

我在弹出窗口中有一个选择框,我可以毫无问题地分配初始值。但是,我想在打开弹出窗口时更改选择框值,但问题是,虽然它“勾选”了更改后的值,但除非我再次打开并关闭弹出窗口,否则它不会在选择框中显示它。有什么解决方案可以使选择框更新,以便当我第一次打开弹出窗口时显示分配的值吗?

注意:在生产中,我使用 angularJs 填充选择框并使用 jqmobile 来渲染它。

这是小提琴和代码:https://jsfiddle.net/AKMorris/ufcasngf/6/

  <button ng-click="openPopup()">open popup</button>
  <div data-role="popup" id="mypopup" data-overlay-theme="d" data-theme="none">
    <select id="fcComparator" 
            ng-model="ccEditorFcComparator"
            ng-options="fcEnumComp for fcEnumComp in ccEditorDefaultComparators"
            >
    </select>
  </div>

js:

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

app.controller('myCtrl', function($scope) {
    $scope.ccEditorDefaultComparators = ["=","!="];
    $scope.ccEditorFcComparator = "!=";

  $scope.openPopup = function()
  {
    $('#mypopup').popup();
    $('#mypopup').popup('open', { y: 0 });

    console.log("should switch to = now");
        $scope.ccEditorFcComparator = "=";

        //this makes it work the second time you open the popup
    $('#fcComparator').selectmenu('refresh');
  }
});
angularjs jquery-mobile drop-down-menu jquery-mobile-popup
2个回答
1
投票

如果您使用 jQuery 函数 (

.option()
) 更改
selectmenu
的选择值,它会正确更新选择框。

Ng-重复解决方案:

JSFiddle 演示

NG选项解决方案:

JSFiddle 演示

注意: 您还需要单独分配 ng-model 变量才能使此修复程序发挥作用。

JS:

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

app.controller('myCtrl', function($scope) {
    $scope.ccEditorDefaultComparators = ["=","!="];
    $scope.ccEditorFcComparator = "!=";
  
  $scope.openPopup = function()
  {
    $('#fcComparator').selectmenu('refresh');
    $('#mypopup').popup();
    $('#mypopup').popup('open', { y: 0 });
    console.log("should switch to = now");
        $('#fcComparator').val("=");
        $scope.ccEditorFcComparator = "=";
        //this makes it work the second time you open the popup
    $('#fcComparator').selectmenu('refresh');
  }
});

参考资料:

  1. jQuery 选择菜单文档

0
投票

是否可以选择为弹出窗口创建另一个控制器并在那里设置所选选项?

HTML

<div data-ng-app='myApp' data-ng-controller='myCtrl'>

  <button id="popup-btn" ng-click="openPopup()">open popup</button>

  <div data-role="popup" id="mypopup" data-overlay-theme="d" data-theme="none" ng-controller="myPopup">
    <select id="fcComparator" 
       ng-model="ccEditorFcComparator"
       ng-options="fcEnumComp for fcEnumComp in ccEditorDefaultComparators">
    </select>
  </div>

</div>

Javascript

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

app.controller('myCtrl', function($scope, $timeout) {
  $scope.openPopup = function()  {
    $('#mypopup').popup();
    $('#mypopup').popup('open', { y: 0 });
  }
});

app.controller('myPopup', function($scope, $timeout) {
    $scope.ccEditorDefaultComparators = ["=","!="];
    $scope.ccEditorFcComparator = "=";
});

https://jsfiddle.net/wjjafupf/

© www.soinside.com 2019 - 2024. All rights reserved.