【问题标题】:angular js ui-select option repeat not working for object arrayangular js ui-select选项重复不适用于对象数组
【发布时间】:2017-12-26 05:42:06
【问题描述】:

我正在使用 Angular 用户界面选择。我的模型和 ui-select 选项数组不同。在更改值时,它不会更新并且不显示选项。我将选定的对象 ID 存储在“pmpo”中,我想在加载时显示 pmptnk 对象数组中的选定对象。但不工作。有人告诉我做错了什么。

我的模型对象

  pmpo:877
  pmptnk:[0:
    632:{id: "632",  pid: "2993", ESID: "9154", lb1: "Undddd", lb2: "219"}
    877:{id: "877",  pid: "2993", ESID: "9154", lb1: "Pcddd", lb2: "29"}
    654:{id: "654",  pid: "2993", ESID: "9154", lb1: "kukuu", lb2: "246"}]

在视图文件中

 <div ng-if="item.pmptnk.length > 0">
    <ui-select ng-model="item.pmpo" click-out-side="closeThis($event)">
    <ui-select-match placeholder="Select " search-placeholder="Filter Tanks" 
    uib-tooltip="{{item.pmpo > 0 ? $select.selected.lb1 : 'Select Tank'}}" tab-select="true">
         <span ng-bind="$select.selected.lb1"></span>
    </ui-select-match>
    <ui-select-choices repeat="obj.tid as obj in (item.pmptnk[item.pmpo])">
        <span ng-bind="obj.lb1"></span>
    </ui-select-choices>
    <ui-select-no-choice>
      No results matched "{{$select.search}}"
    </ui-select-no-choice>
    </ui-select>

</div>

【问题讨论】:

    标签: javascript angularjs ui-select


    【解决方案1】:

    我处理了您的代码。我以不同的方式进行了尝试。 下面是我的代码sn-p:

    <div ng-if="item.pmptnk.length > 0">
            <ui-select ng-model="item.selected" click-out-side="closeThis($event)">
               <ui-select-match placeholder="Select " search-placeholder="Filter Tanks" uib-tooltip="{{item.pmpo > 0 ? $select.selected.lb1 : 'Select Tank'}}" tab-select="true">
                  <span ng-bind="$select.selected.lb1"></span>
               </ui-select-match>
               <ui-select-choices repeat="obj.tid as obj in (item.pmptnk)">
                  <span ng-bind="obj.lb1"></span>
               </ui-select-choices>
           <ui-select-no-choice>
                No results matched "{{$select.search}}"
           </ui-select-no-choice>
       </ui-select>
    

    我改变了我的模型如下:

    $scope.item = {};
        $scope.item.pmpo=877;
        $scope.item.pmptnk=[
        {id: "632",  pid: "2993", ESID: "9154", lb1: "Undddd", lb2: "219"},
        {id: "877",  pid: "2993", ESID: "9154", lb1: "Pcddd", lb2: "29"},
        { id: "654", pid: "2993", ESID: "9154", lb1: "kukuu", lb2: "246" }];
        for (var i = 0 ; i < $scope.item.pmptnk.length; i++) {
            if ($scope.item.pmptnk[i].id == $scope.item.pmpo) {
                $scope.item.selected = $scope.item.pmptnk[i].tid;
                break;
            }
        }
    

    这对我来说很好。

    【讨论】:

      【解决方案2】:

      根据docs of ui-select-choicesrepeat 属性

      指定要作为选项提供的项目列表。语法相似 到 ngRepeat

      并根据ng-repeat doc

      可以让 ngRepeat 迭代一个 使用以下语法的对象:

      <div ng-repeat="(key, value) in myObj"> ... </div>
      

      因此,我们可以得出结论,您应该更改您的语法

      <ui-select-choices repeat="obj.tid as obj in (item.pmptnk[item.pmpo])">
      

      到此

      <ui-select-choices repeat="(key, value) in (item.pmptnk[item.pmpo])">
      

      其中value 将是8772993 等等,key 将是idpid 等等。

      【讨论】:

      • 感谢您的回复。我试过你说的。没有运气。
      • 确保(item.pmptnk[item.pmpo])实际上是一个对象。
      • 在我的问题中,我展示了 item.pmptnk 的结构。 item.pmptnk 是一个对象数组,键为 item.pmpo
      • 此消息表明这可能是以下两种情况之一:一个repeat 属性语法类似于 ngRepeat,但绝对不接受迭代的能力对象的属性(不像ngRepeat)或两个item.pmptnk[item.pmpo] 的结构存在一些错误。如果是第一种情况,ui-select 的这些人应该在文档中更清楚地说明这一点,您将不得不更改item.pmptnk[item.pmpo] 的结构。如果不是这样,您应该更改 item.pmptnk[item.pmpo] 的结构以解决此问题。
      【解决方案3】:

      不应该是 ng-repeat 而不是只是重复吗?

       <ui-select-choices repeat="obj.tid as obj in (item.pmptnk[item.pmpo])">
      

      将此行更改为以下行

      <ui-select-choices ng-repeat="obj.tid as obj in (item.pmptnk[item.pmpo])">
      

      希望能解决这个问题。

      【讨论】:

      • 你是在问还是在回答?
      • @abdulAleem Khan 重复将起作用。问题是关于对象数组循环。如果我将数组更改为简单对象,它的工作原理。
      • 根据docs 应该是repeat,而不是ng-repeat
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 2014-02-23
      • 2018-04-12
      • 2016-02-19
      • 2017-07-03
      • 1970-01-01
      相关资源
      最近更新 更多