2016-03-22 13 views
0

Angularの新機能で、$ httpサービス経由でdbからデータにアクセスしていて、db内の一致するテキストエリアコンテンツの値を置き換えます。AngularJS:foreachループ内のテキストを置き換えます。

app.controller('myController', function($scope, $http) { 
    $scope.translate = function() { 
      $http 
      .get('translate.php') 
      .then(function(data){ 
       var alldata = data.data; 
       angular.forEach(alldata, function(v,k) { 
         $scope.message = alldata.replace("\\b"+v.one+"\\b/gi",v.two); 
       }); 
      }, function(data) { 
       // error handling 
     }); 
    }; 
}) 

textareaには「メッセージ」のng-modelがあります。それは働いていないと私はエラーを取得しています:

TypeError: alldata.replace is not a function 
+0

あなたはALLDATAが文字列であることを確認することができ、これを試してみてください? – user2263572

+0

@ user2263572 ahh、alldataは配列です。私はそれを$ scope.message.replaceに変更しましたが、今は 'TypeError:プロパティを読み取れません 'というエラーが発生しました... –

答えて

0

app.controller('myController', function($scope, $http) { 
    $scope.message = ''; 
    $scope.translate = function() { 
      $http 
      .get('translate.php') 
      .then(function(data){ 
       var alldata = data.data; 
       angular.forEach(alldata, function(v,k) { 
         $scope.message = $scope.message.toString().replace("\\b"+v.one+"\\b/gi",v.two); 
       }); 
      }, function(data) { 
       // error handling 
     }); 
    }; 
}) 
+0

' TypeError:プロパティ' toString 'undefined'エラーを読み取ることができません。 $ scope.messageを認識していないようですか? –

+0

私は参照してください。私は少し答えを編集します。 –

+0

あなたは$ scope.messageがバットから離れた文字列であることを確認したいかもしれません。答えを編集しました。 –

関連する問題