2016-12-15 69 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。我已更新我的問題。 –