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

时间:2017-08-31 00:27:40

标签: angularjs jquery-mobile drop-down-menu jquery-mobile-popup

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

注意:在制作中我使用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');
  }
});

2 个答案:

答案 0 :(得分:1)

如果使用.option()的JQuery函数(selectmenu)更改select的值,它会正确更新选择框。

Ng-repeat解决方案:

JSFiddle Demo

Ng-options解决方案:

JSFiddle Demo

注意:您还需要分别为此修补程序分配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 Select Menu Documentation

答案 1 :(得分: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/