Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用sweetalert时,html下拉列表不反映新条目_Javascript_Html_Angular_Sweetalert - Fatal编程技术网

Javascript 使用sweetalert时,html下拉列表不反映新条目

Javascript 使用sweetalert时,html下拉列表不反映新条目,javascript,html,angular,sweetalert,Javascript,Html,Angular,Sweetalert,我有一个HTML下拉列表,下拉列表中已经有一些条目。现在,如果用户选择了一个名为“New Version”的选项,我会打开一个提示框,询问versionName,并将其推送到下拉列表项中,然后从下拉列表中选择新输入的选项作为默认选项。现在,当我使用常规javascript警报框时,一切都进展顺利。新输入的版本是从下拉列表中选择的选项。 然而,当我开始使用sweetalert时,这种行为就不再存在了。当用户选择“新版本”时,会打开sweetalert框,用户输入新版本名称,按ok,但下拉列表仍将所

我有一个HTML下拉列表,下拉列表中已经有一些条目。现在,如果用户选择了一个名为“New Version”的选项,我会打开一个提示框,询问versionName,并将其推送到下拉列表项中,然后从下拉列表中选择新输入的选项作为默认选项。现在,当我使用常规javascript警报框时,一切都进展顺利。新输入的版本是从下拉列表中选择的选项。 然而,当我开始使用sweetalert时,这种行为就不再存在了。当用户选择“新版本”时,会打开sweetalert框,用户输入新版本名称,按ok,但下拉列表仍将所选值显示为“新版本”,我在下拉列表中看不到新版本名称。只有当我从下拉列表中选择另一个选项时,我才能在下拉列表中看到新版本,该选项基本上“刷新”下拉列表,然后我才能看到我的新值

相应的角度代码(使用工作正常的javascript警报):

Sweetalert逻辑:

$scope.onSetDecisionVersion = function() {
    if($scope.requestDetails.decisionVersion == 'New Version') {

        swal({
            text:'Enter new version name:',
            content:'input',
            button: {
                text:"Enter",
                closeModal:true,
            },
        }).then(versionName => 
        this.updateDropDownList(versionName));  
    }
} 

 $scope.updateDropDownList = function(versionName) {
        $scope.requestDetails.decisionVersion = versionName;
        $scope.dcnVersions.push(versionName);

    }
html代码:

<label class="col-sm-4 col-form-label">Version</label>
<div class="col-sm-2">
    <select required="required" ng-options="versionName for versionName in dcnVersions"  class="wd100" ng-model="requestDetails.decisionVersion" ng-change='onSetDecisionVersion()'>
        <option ng-selected="true" value="">Select</option>
    </select>
</div>
版本
挑选
我已经注释掉了代码的sweetalert部分。javascript警报框代码就在那里,并且按照预期工作。我无法理解为什么下拉列表不能立即反映新输入的sweet alert版本。这是我用于sweetalert的CDN:

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>


此外,当我使用sweetalert逻辑将此requestDetails.decisionVersion发送到后端时,reguestDetails.decisionVersion读取正确的值,但由于任何原因,正确的值不会反映在下拉列表中。

您需要使用$timeout,因为angular js具有不同的计时模式

以下是您的工作JSFIDLE代码:

代码很好。。您需要像这样使用$timeout

$timeout(function(){
            $scope.option = text;
          })

注释在浏览器中被忽略,因此它不起作用,或者您是否对其进行了注释以确保我们知道它的哪一部分?我注释掉了onSetDecisionVersion函数中使用swal的逻辑您是否无法看到注释掉的代码?我可以在我的结尾看到注释被忽略,对于输出,它现在没有更新,我添加了javascript alertbox逻辑和SweetAlertBox逻辑
var app = angular.module('sweetalert', []);

app.controller('sacontroller', function($scope, $timeout){
$scope.option = "Select One";
    $scope.options = ["Select One","a", "b", "New Version"];
  $scope.onOptionChange = function(){
  if($scope.option !== "New Version") return;
    onNewVersionSelect();
  }

  function onNewVersionSelect(){
   swal({
            text:'Enter new version name:',
            content:'input',
            button: {
                text:"Enter",
                closeModal:true,
            },
        }).then((text) =>{
            $scope.options.push(text);
          $timeout(function(){
            $scope.option = text;
          })
        })
  }


});
$timeout(function(){
            $scope.option = text;
          })