2016-07-27 4 views
1

を交換する私は、次の形式でオブジェクトを返した:反復、マッチを見つけて

[ 
    { 
     "category": "Coach", 
     "phrase_original": "Training {{the team}} to develop their match skills by ensuring they are comfortable with {{defence techniques}}", 
     "phrase_filter": "Training {{group}} to develop their {{attribute}} skills by ensuring they are comfortable with {{factor}}" 
    }, 
    { 
     "category": "Coach", 
     "phrase_original": "Assisting the {{fitness coach}} in strength and conditioning work to improve {{team performance}}", 
     "phrase_filter": "Assisting the {{person}} in strength and conditioning work to improve {{factor}}" 
    } 
] 

私はそれぞれを解析し、交換したいと思います:

phrase_filter
  • {{group}}<span style="attribute-button">group</span>
  • <span style="group*button">group</span>phrase_filter
  • {{attribute}}
  • これを達成する最良の方法だろう何 <span style="person-button">person</span>

phrase_filter<span style="factor-button">group</span>

  • {{person}}phrase_filterで?

    これはこれまでのコードですが、上記を実装するかどうかはわかりません。私は現在、APIエンドポイントからデータを取得していますが、まだ処理する方法がわかりません。

    CategoryService.getCategoryDetail($scope.categoryId).then(function(dataResponse) { 
        $scope.categoryDetail = dataResponse.data; 
        angular.forEach($scope.categoryDetail, function(e) { 
        // 1. find all the words in braces in phrase_filter 
        // 2. replace them with html markup so they are rendered in the view differently 
        // e.phrase_filter = ??? 
        }); 
    }); 
    
  • +1

    anglejsをタグとして追加する必要がありますが、 JS –

    答えて

    1

    あなたはそのような何か試すことができます。

    var inputs = [ 
     
        { 
     
        "category": "Coach", 
     
        "phrase_original": "Training {{the team}} to develop their match skills by ensuring they are comfortable with {{defence techniques}}", 
     
        "phrase_filter": "Training {{group}} to develop their {{attribute}} skills by ensuring they are comfortable with {{factor}}" 
     
        }, 
     
        { 
     
        "category": "Coach", 
     
        "phrase_original": "Assisting the {{fitness coach}} in strength and conditioning work to improve {{team performance}}", 
     
        "phrase_filter": "Assisting the {{person}} in strength and conditioning work to improve {{factor}}" 
     
        } 
     
    ] 
     
    
     
    var replacers = { 
     
        '{{group}}' : '<span style="group-button">group</span>', 
     
        '{{attribute}}' : '<span style="attribute-button">attribute</span>', 
     
        '{{factor}}' : '<span style="factor-button">factor</span>', 
     
        '{{person}}' : '<span style="person-button">person</span>' 
     
    } 
     
    
     
    // Loop through every objects 
     
    inputs.forEach(function(input){ 
     
        Object.keys(replacers).forEach(function(key){ 
     
        // Replace every occurence of this key 
     
        input['phrase_filter'] = input['phrase_filter'].replace(new RegExp(key, 'g'), replacers[key]); 
     
        }) 
     
    }) 
     
    
     
    console.log(inputs);

    をしかし、私はそれが最善の解決策は、あなたが...あなたがすべき角度で動作するという事実をregardindだかわかりませんカスタムディレクティブなどを作成する方が効率的です;)うまくいきたいです

    関連する問題