字符串替换为参数值Angular

时间:2016-06-20 19:34:23

标签: javascript html angularjs

我正在尝试使用格式化的字母填充文本区域,该字母来自文本文件。此文本文件包含{{client.name}}{{client.address}}等对象,我希望将其替换为特定客户端属性的值。这是我到目前为止的代码,

$scope.loadFlyer = function() {
    $http.get("/assets/clientwelcome.txt").then(function(res){
        $scope.flyerdescription = res.data;
        $scope.flyerdescription = $scope.flyerdescription.replace(/{{client.name}}/g, $scope.client.name); 
  });
};

之前我曾从客户的表中调用数据:

myApp.controller('ClientCtrl', ['$scope', '$http', '$routeParams', function($scope, $http, $routeParams) {
$scope.clients = [];
$http.get('/client').success(function(data, status, headers, config) {
    $scope.clients = data;
    if (data == "") {
        $scope.clients = [];
    }
}).error(function(data, status, headers, config) {
    console.log("Ops: could not get any data");
});

这是我想要填充的领域:

<div class="form-group">
    <label class="control-label" for="flyer-description">Description</label>
    <textarea style="height:400px" class="form-control" id="flyer-description" ng-model="flyerdescription"></textarea>
</div>

不幸的是,没有替换。正如我在javascript文档中看到的那样,我试图对替换进行格式化,但无济于事。

2 个答案:

答案 0 :(得分:0)

按照下面的讨论编辑*

您有一个客户列表,如[{name:&#39; Hank Aaron&#39;},{name:&#39; Sandy Koufax&#39;},{name:&#39; Bo Jackson& #39;}]和一个模板字符串,其中包含一个值:&#39; {{client.name}}&#39;,您希望将其替换为客户列表中的名称。

myApp.controller('ClientCtrl', ['$scope', '$http', '$routeParams', function($scope, $http, $routeParams) {
    $scope.clients = [];
    $http.get('/client').success(function(data, status, headers, config) {
        $scope.clients = data;
        if (data == "") {
            $scope.clients = [];
        }
    }).error(function(data, status, headers, config) {
        console.log("Ops: could not get any data");
    });

    $scope.loadFlyer = function() {
        $http.get("/assets/clientwelcome.txt").then(function(res){
            $scope.flyerdescription = res.data;
            $scope.descriptions = [];
            for(var i=0; i<$scope.clients.length; i++){
                $scope.descriptions[i].push($scope.flyerdescription.replace(/{{client.name}}/g,$scope.clients[i].name));
            }
        });
    };
}]);

并在您的HTML中

<div class="form-group">
    <div ng-repeat="description in descriptions">
        <label class="control-label" for="flyer-description">Description</label>
        <textarea style="height:400px" class="form-control" id="flyer-description" ng-model="description"></textarea>
    </div>
</div>

答案 1 :(得分:0)

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
</head>
<body>
<div ng-app="demoApp" ng-controller="validationCtrl"  class="form-group">
    <label class="control-label" for="flyer-description">Description</label>
    <textarea style="height:100px" class="form-control" id="flyer-description" ng-model="stringname
"></textarea>
</div>
<script>
//This is controller
var app = angular.module('demoApp', []);
app.controller('validationCtrl', function($scope) {
    $scope.stringname = 'angular123456 test demo';
    $scope.stringname = $scope.stringname.replace('test', "");
});
</script>
</body>
</html>