2016-12-15 65 views
0

帽子是我如何拥有多个值,我将发送它以继续喜欢客户可以创建的用户。发送数组 - 无法读取undefined的属性'map'

我在这里找到:https://stackoverflow.com/a/14520103/7180653与对象数组作为输入数据

我根据他的代码需要,这是我应该怎么只有这是真正的知识的价值。

当我看着我的按钮。所以我得到这个错误

Array[0]

TypeError: Cannot read property 'map' of undefined

HTML - 视图:

<ul style="list-style-type:none;"> 
    <li class="col-md-4" ng-repeat="Value in ColorAddUppers"> 
     <img ng-src="{{Value.Image}}" class="img-responsive img-rounded mb-lg" /> 
     <p class="label label-lg label-primary full-width"> 
      <input type="checkbox" 
        ng-model="Value.selected" 
        name="selectedColorUppers[]" 
        value="{{Value.IDColor}}" /> 
      {{Value.Text}} 
     </p> 
    </li> 
</ul> 

CreateUser.js

var url = "/JsonClass/ColorAddToUppers"; 
$http.get(url).success(function (response) { 
    $scope.ColorAddUppers = response; 
}); 


//Overdel 
$scope.NextLvlThree = function() { 
    //Check Color Values 

    $scope.selectionColorUppersView = []; 
    $scope.selectedColorUppers = function selectedColorUppers() { 
     return filterFilter($scope.ColorAddUppers, { selected: true }); 
    }; 
    $scope.$watch('fruits|filter:{selected:true}', function (nv) { 
     $scope.selectionColorUppersView = nv.map(function (ColorNameUppersText) { 
      return ColorNameUppersText.Text; 
     }); 
    }, true); 

    console.log($scope.selectionColorUppersView); 
} 

EIDT(更新)

enter image description here

当我看着这张照片时,就像它没有得到我的价值。

我已经在代码中使用这样的:

$scope.selectedTextValuesByWatch = []; 

    $scope.$watchCollection('ColorAddUppers|filter:{selected:true}', function (nv, ov) { 
     $scope.selectedTextValuesByWatch = nv.filter(function (value) { 
      return value.selected; 
     }).map(function (value) { 
      return value.Text; 
     }); 
    }, true); 

    $scope.getSelectedTextValues = function() { 
     return $scope.ColorAddUppers.filter(function (value) { 
      return value.selected; 
     }).map(function (value) { 
      return value.Text; 
     }); 
    } 

    console.log($scope.selectedTextValuesByWatch); 

    if($scope.selectedTextValuesByWatch.length >= 1) 
    { 
     console.log("check"); 
    } 
    else 
    { 
     console.log("error"); 
    } 
+0

哦,男人,这是英语吗?你确定'fruits'是在应用程序的某个地方定义的吗? –

+0

@AbbéRésinaohh我需要删除'fruits'我试图说:'$ scope。$ watch('Value | filter:{selected:true}',' –

+0

$ scope。$ watch只返回对象,尝试使用$范围。$ watchCollection –

回答

0

我不知道,当你调用NextLvlThree()功能,但$watch的声明应该是在控制器级别,而不是在这个函数内,以避免多重声明。

无论如何,有几种方法来获取列表中选定的值:

使用视图中的动态过滤器,例如

<pre>{ColorAddUppers|filter:{selected:true}|json}}</pre> 

使用的手表,将更新仅包含每个值的Text选择的列表:

$scope.$watchCollection('ColorAddUppers|filter:{selected:true}', function (nv, ov) { 
    $scope.selectedTextValuesByWatch = nv.filter(function (value) { 
     return value.selected; 
    }).map(function(value) { 
     return value.Text; 
    }); 
}, true); 

或者只是使用,做同样的过滤功能应为JSON显示选择值如手表,但是当一些按钮,用户点击:

$scope.getSelectedTextValues = function() { 
    return $scope.ColorAddUppers.filter(function(value) { 
     return value.selected; 
    }).map(function(value) { 
     return value.Text; 
    }); 
} 

查看所有3行动this plunker here

+0

这就是我认为是应该的。感谢您的帮助,我有几个问题,我会马上回来,否则我创建一个新的。祝你好日子 –

+0

HeyAbbéRésina。我已更新我的问题。 –