【问题标题】:Multiple arrays mapping angular JS多个数组映射角度JS
【发布时间】:2014-06-11 11:58:44
【问题描述】:

尝试在jquery each 方法中添加两个数组,并尝试根据逻辑显示不同的文本。

我有两个数组,demo 和 catID。演示数组工作正常,当进入 catID 数组时 jquery .each 无法正常工作,它仍然返回一个数组。那就是条件失败了,它无法从数组中找出完全匹配的值。谁能帮帮我?

JS:

    function App( ){
                var Label = ‘a’;
        var KeyLabel = 'cat'; 
                var demo = [“a”,”b”,’c’,’d’];
                var CatID =['1','2','3'];
scatID='1';
                jQuery.each( [demo,CatID], function( index, value) {
                    debugger;
                    if((Label==value)&&(scatID==CatID)){
                        debugger;
                        $scope.snewText = 'Hello from A';
                        $scope.scatText = 'Cat ID';
                    }
                });
            }

Fiddle

【问题讨论】:

    标签: javascript jquery arrays angularjs


    【解决方案1】:

    这就是你想要的吗? jsfiddle

    var myApp = angular.module('myApp', []);
    
    myApp.controller('MainCtrl', ['$scope', function ($scope) {
    
        var Label = 'a';
        var KeyLabel = 'cat';
        var demo = ['a', 'b', 'c', 'd'];
        var CatID = ['1', '2', '3'];
        scatID = '1';
        angular.forEach(demo, function (value, index) {
    
            if (Label == value) {
    
                $scope.snewText = 'Hello from A';
                $scope.scatText = 'Cat ID';
            }
        });
    
    }]);
    

    【讨论】:

      【解决方案2】:

      我在 jquery 文档的任何地方都找不到每个的定义

      http://api.jquery.com/each/

      更不用说 Angular 有自己的 foreach 更适合这种类型的东西

      https://docs.angularjs.org/api/ng/function/angular.forEach

      如果你想要做的是检查 Label 和 scatID 是否属于同一个索引,你最好做类似的事情

          if(catID.indexOf(scatID)===demo.indexOf(label)){
                 $scope.snewText = 'Hello from A';
                 $scope.scatText = 'Cat ID';
           }
      

      【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2023-03-14
      • 2021-04-06
      • 2012-08-28
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      相关资源
      最近更新 更多