【问题标题】:equating variables which are coming from http calls等同于来自 http 调用的变量
【发布时间】:2019-05-05 10:08:48
【问题描述】:

我有一个选择标签,其中包含由 AngularJS 填充的选项。如果它等于范围内的另一个属性,我正在尝试选择一个选项。我试图比较的选项值和范围属性都来自异步 http 调用。所以总是有延迟,然后它就不能正常工作。确保两个范围属性都已解析并准备好进行比较的最佳做法是什么。

ng-selected="MusteriId == option.Value" 正在比较部分。

<select id="MusteriId" name="MusteriId" ng-model="MusteriId">
    <option ng-selected="MusteriId == option.Value"
            ng-repeat="option in MusteriList" value="{{option.Value}}">
      {{option.Text}}
    </option>
</select>

这是执行两个 http 调用的控制器。

(function() {
var biletController = function ($scope, $http, commonFunctions) {

    $scope.Id = null;
    $scope.BiletNo = null;
    $scope.BiletTarihi = null;
    $scope.CurrencyId = null;
    $scope.MusteriId = null;
    $scope.PAID_EUR = null;
    $scope.PAID_TL = null;
    $scope.PAID_USD = null;
    $scope.ServisIstiyorMu = null;
    $scope.TOTAL = null;
    $scope.TourId = null;

    $scope.MusteriList = null;

    $scope.openEditFormJS = function(e) {
        $http.get('/Bilet/Get/' + e)
            .then(function (response) {

                console.log(response.data);

                $scope.Id = response.data.Id;
                $scope.BiletNo = response.data.BiletNo;

                if (response.data.BiletTarihi) {
                    $scope.BiletTarihi = commonFunctions.formatDate(new Date(parseInt(response.data.BiletTarihi.substr(6))));
                }

                $scope.CurrencyId = response.data.CurrencyId;
                $scope.MusteriId = response.data.MusteriId;
                $scope.PAID_EUR = response.data.PAID_EUR;
                $scope.PAID_TL = response.data.PAID_TL;
                $scope.PAID_USD = response.data.PAID_USD;
                $scope.ServisIstiyorMu = response.data.ServisIstiyorMu;
                $scope.TOTAL = response.data.TOTAL;
                $scope.TourId = response.data.TourId;

                $('#modal').modal('show');

            });

        $http.get('/Bilet/GetMusteriSelectList')
            .then(function (response) {

                console.log(response.data);
                $scope.MusteriList = response.data;
            });
    };

};

app.controller('BiletController', ['$scope', '$http', 'commonFunctions', biletController]);
}());

【问题讨论】:

  • 为了更好地理解问题,我需要从两个 $http 请求中查看response.data 的示例。

标签: angularjs ng-options angularjs-select


【解决方案1】:

对非字符串值使用ng-value 指令1

<select id="MusteriId" name="MusteriId" ng-model="MusteriId">
    <option ̶n̶g̶-̶s̶e̶l̶e̶c̶t̶e̶d̶=̶"̶M̶u̶s̶t̶e̶r̶i̶I̶d̶ ̶=̶=̶ ̶o̶p̶t̶i̶o̶n̶.̶V̶a̶l̶u̶e̶"̶
            ng-repeat="option in MusteriList" ng-value="option.Value">
      {{option.Text}}
    </option>
</select>

更多信息请见Using ngValue to bind the model to an array of objects


不要将ngSelectedngModel 一起使用2

<select id="MusteriId" name="MusteriId" ng-model="MusteriId">
    <option ̶n̶g̶-̶s̶e̶l̶e̶c̶t̶e̶d̶=̶"̶M̶u̶s̶t̶e̶r̶i̶I̶d̶ ̶=̶=̶ ̶o̶p̶t̶i̶o̶n̶.̶V̶a̶l̶u̶e̶"̶
            ng-repeat="option in MusteriList" value="{{option.Value}}">
      {{option.Text}}
    </option>
</select>

来自文档:

注意:ngSelected 不与 &lt;select&gt;ngModel 指令交互,它只设置元素的 selected 属性。如果您在选择上使用ngModel您不应在选项上使用ngSelected,因为ngModel 将设置选择值和选定选项。

——AngularJS ng-selected API Reference

查看其他文档:

查看 Stackoverflow:


【讨论】:

  • 谢谢,您的解决方案只有在我将 http 响应中的 MusteriId 结果转换为字符串时才有效。如果我可以实现将 http 结果自动转换为字符串的东西,您有什么建议吗?这是我使用的> $scope.MusteriId = String(response.data.MusteriId);
  • 你用的是value="{{option.Value}}"还是ng-value="option.Value"
  • 我按照您的建议使用了 ng-value="option.Value"。它工作正常,但对数据类型、字符串或数字很敏感。我应该使用转换为 String 来使其工作,这很有意义。
  • &lt;select&gt; 指令在将 ng-model 与 value 属性进行比较时使用严格相等 (===)。这意味着true,"true",0,false,"0",77,和"77"都可以有单独的选择选项。如果一个服务器请求返回数字 id 而另一个请求返回字符串 id,这是后端编码人员的不幸设计决定。
猜你喜欢
  • 2022-11-17
  • 2011-08-31
  • 2018-10-20
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多