Php AngularJS:替换foreach循环中的文本

Php AngularJS:替换foreach循环中的文本,php,angularjs,http,service,foreach,Php,Angularjs,Http,Service,Foreach,Angular新手,我正在通过$http服务从db访问数据,并希望替换文本区域中的值,该文本区域中的值与db中找到的单词内容匹配 app.controller('myController', function($scope, $http) { $scope.translate = function() { $http .get('translate.php') .then(function(data){

Angular新手,我正在通过$http服务从db访问数据,并希望替换文本区域中的值,该文本区域中的值与db中找到的单词内容匹配

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
         });
     };
 })
text区域具有“消息”的
ng型号
。它不工作,我得到一个错误:

TypeError: alldata.replace is not a function
试试这个

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
         });
     };
 })

你能确认alldata是一个字符串吗?@user2263572啊,alldata是一个数组。我将其更改为$scope.message.replace,但现在我得到
TypeError:无法读取未定义的属性“replace”
错误…
TypeError:无法读取未定义的属性“toString”
错误。看起来它无法识别$scope.message?您可能需要确保您的$scope.message是一个现成的字符串。编辑了答案。如果出现错误。一起删除“toString()”。太棒了,谢谢。另外,仅供参考,除非我使用regex对象,否则替换不起作用:
.replace(新的RegExp(“\\b”+v.one+“\\b”,“gi”),v.two)
啊,好的,很有趣