2017-04-17 9 views
0

目的:$ scope .pluraliserは、年が選択されたときにランダムな値を返すようにします。 1966 現在、複数の値が同じ場合、常に最後の値が選択されます。フィッシャーイエーツシャッフル配列アルゴリズムAngularJS ng-pluralizerからランダムな値を取得する方法は?

を使用して

の$ scope.pluralizer = {

1966 : "Something New !", 
    1966 : "Something Different!", 
    1966 : "Something Else", 
    1967 : "Something New !", 
    1967 : "Something Different!"', 
    1967 : "Something Else", 
    1968 : "Something New !", 
    1968 : "Something Different!", 
    1968 : "Something Else" 


    } 


You can randomise nested lists quite easily with ng-repeat 

http://jsfiddle.net/owenmead/fa4v8/1/ 

$ scope.list = [

'"1966" : "Something New !"', 
    '"1966" : "Something Different!"', 
    '"1966" : "Something Else"', 
    '"1967" : "Something New !"', 
    '"1967" : "Something Different!"', 
    '"1967" : "Something Else"', 
    '"1968" : "Something New !"', 
    '"1969" : "Something Different!"', 
    '"1970" : "Something Else"', 

] 
$scope.random = function() { 
    return 0.5 - Math.random(); 

    HTML 
    <p ng-repeat="i in list|orderBy:random">{{i}}</p> 

とランダム化配列はどのように私はそうPluralizerをランダム化することができ年が選択されたときに異なる/ランダムな値を返しますか?

答えて

0

pluralizer変数には、 "1966"の値を上書きしています。したがって、3つの値を定義している間は、それぞれが前の値を上書きします。

これを解決する1つの方法は、このような配列で複数の値を格納することである。これにより

$scope.pluralizer = { 
    1966 : ["Something New!", "Something Different!", "Something Else"],  
    1967 : ["Something New!", "Something Different!", "Something Else"], 
    1968 : ["Something New!", "Something Different!", "Something Else"] 
} 

あなたは1966年のための配列を取得して、ランダムに配列インデックスのいずれかを選択することができます。どのようにこの変数を使用するかわからないので、値をランダムに抽出するためのコードを追加しません。

関連する問題