【问题标题】:How to dynamically add element with tabs in the list. Angular js如何在列表中动态添加带有选项卡的元素。角度js
【发布时间】:2016-08-26 07:46:28
【问题描述】:

我有一个问题,如何在列表中动态添加带有标签的元素。

问题是,如果我尝试加载带有设备信息的用户选项卡列表,则会被最后一个用户覆盖。

这是我的 HTML 标签项:

<div class="sample tabsdemoDynamicTabs" layout="column" ng-cloak="">
            <md-content class="md-padding">
            <md-tabs  md-border-bottom="" md-autoselect="">
                <md-tab ng-repeat="tab in tabs" ng-disabled="tab.disabled" label="{{tab.title}}">
                <div class="demo-tab tab{{$index%4}}" style="padding: 25px; text-align: center;">
                    <div class="cardContentSmall" ng-bind="tab.content.objectId"></div> 
                    <div class="cardContentSmall" ng-bind="tab.content.password"></div>
                    <div class="cardContentSmall" ng-bind="tab.content.version"></div>
                    <div class="cardContentSmall" ng-bind="tab.content.mac"></div>
                    <div class="cardContentSmall" ng-bind="tab.content.serialNumber"></div>
                    <div class="cardContentSmall" ng-bind="tab.content.createdAt"></div>
                    <div class="cardContentSmall" ng-bind="tab.content.updatedAt"></div>                                
                </div>
                </md-tab>
            </md-tabs>
            </md-content>                       
</div>

还有我的控制器:

app.controller('SearchController', function($scope, $state, $compile, $element, searchService, toastService) {
if(Parse.User.current()) {
  $scope.$parent.title = "Search";
  $scope.$parent.imageSrc = "images/icon/search_icon.png";
}

$scope.searchService = function(keyword, param1) { 
      searchService.find(keyword, param1, function(response, error) {
        if(response) {
              $scope.$apply(function(){                 
                 $scope.response = ParseArrayToJSON(response);
                 for(var i = 0; i < response.length; i++){
                    var userDevices = [];
                    if(response[i].get('devices') != null){
                        var devices = ParseArrayToJSON(response[i].get('devices'));
                        console.log("devices: " + devices.length);                          
                            for(var j = 0; j < devices.length; j++){                                    
                                var device = devices[j];                         
                                var deviceCount = "Device " + j;
                                var content = {
                                    "objectId" : "Object ID: "  + device["objectId"],
                                    "password" : "Password: " + device["password"],
                                    "version" : "Version: " + device["version"],
                                    "mac" : "MAC address: " + device["mac"],
                                    "serialNumber" : "Serial number: " + device["serial_number"],
                                    "createdAt" : "Created at: " + device["createdAt"],
                                    "updatedAt" : "Updated at: " + device["updatedAt"]                                      
                                }
                                var item = {
                                    "title" : deviceCount, 
                                    "content" : content
                                }                    
                                userDevices.push(item);
                            }       

                        $scope.tabs = userDevices;      

                    }                    
                 }                           
          });         
        } else {
        toastService.show("No results");
      }      
    });
  };
});

【问题讨论】:

    标签: javascript jquery angularjs angularjs-directive


    【解决方案1】:

    您的视图中似乎有ng-repeat="tab in tabs"。很难弄清楚 json response,但我猜它包含搜索返回的多个用户的数据。如果是这样,而不是分配$scope.tabs = userDevices;,你应该有一个二维数组,比如:$scope.tabs[user_id] = userDevices;

    然后在视图中,您可以遍历用户特定的选项卡:ng-repeat="tab in tabs[user_id]"

    我希望它有所帮助,尽管您提出的问题没有足够的细节来了解您的问题到底出在哪里。

    【讨论】:

    • 谢谢你,因为我看到你理解我的问题是正确的,我已经按照你说的做了 $scope.tabs[currentUser] = userDevices;和 ng-repeat="tab in tabs[{{item.objectId}}]" 但我收到错误“angular.js:13283 TypeError: Cannot set property 's5xIgWU0kM' of undefined”,s5xIgWU0kM 是我的用户 ID。我对二维数组没有任何经验:(
    • @user2996120 只是在$scope.searchService 方法之前初始化$scope.tabs = {}
    • 现在如果我设置 ng-repeat="tab in tabs[{{item.objectId}}]" 我得到错误:语法错误:表达式第 7 列的令牌'{'无效键 [选项卡 [{{item.objectId}}]] 从 [{item.objectId}}]] 开始。如果我在选项卡 [s5xIgWU0kM] 中设置 ng-repeat="tab,则没有错误但选项卡为空:(
    • ng-repeat="tab in tabs['s5xIgWU0kM'] 工作正常,但我需要找出为什么 ng-repeat="tab in tabs[{{item.objectId}}]"不起作用。
    • 试试ng-repeat="tab in tabs[item.objectId]"
    猜你喜欢
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2020-03-21
    • 2020-12-22
    • 1970-01-01
    相关资源
    最近更新 更多