Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS-绑定/替换AJAX内容_Javascript_Ajax_Angularjs - Fatal编程技术网

Javascript AngularJS-绑定/替换AJAX内容

Javascript AngularJS-绑定/替换AJAX内容,javascript,ajax,angularjs,Javascript,Ajax,Angularjs,我使用AngularJS加载AJAX内容,并使用ng repeat创建项目列表。在内容上,我还有{{noun}}作为占位符。我的假设是,当加载ajax内容时,AngularJS将自动用$scope.noun模型中的数据替换{{noon}}。但事实并非如此。有什么快速而肮脏的方法可以让它发生吗 这是我的密码: AllControllers.controller('AppController', ['$scope', '$http', function ($scope, $http) {

我使用AngularJS加载AJAX内容,并使用ng repeat创建项目列表。在内容上,我还有{{noun}}作为占位符。我的假设是,当加载ajax内容时,AngularJS将自动用$scope.noun模型中的数据替换{{noon}}。但事实并非如此。有什么快速而肮脏的方法可以让它发生吗

这是我的密码:

AllControllers.controller('AppController', ['$scope', '$http', function ($scope, $http) {

    $scope.noun = "My Noun";

    $scope.headlines = [{
        headline: "Top 10 Tricks to {{noun}}",
        usage: 10,
        votes: 100
    }];

    $scope.load_headlines = function() {
        $http.get('/data/headlines.json').
            success(function(data, status, headers, config){
                $scope.headlines = data;
            }).
            error(function(data, status, headers, config){
                console.log(status);
            });
    };
    }]);

<div ng-controller="AppController" ng-init="load_headlines()">

    <table>
    <tbody ng-repeat="headline in headlines">
    <tr>
        <td>{{headline.headline}}</td>
        <td class="center">{{headline.usage}}</td>
        <td class="center">{{headline.votes}}</td>
    </tr>
    </tbody>
    </table>

</div>
AllControllers.controller('AppController',['$scope','$http',函数($scope,$http){
$scope.noun=“我的名词”;
$scope.headlines=[{
标题:“十大{{名词}}诀窍”,
用法:10,
票数:100
}];
$scope.load_headlines=函数(){
$http.get('/data/headlines.json')。
成功(函数(数据、状态、标题、配置){
$scope.headlines=数据;
}).
错误(函数(数据、状态、标题、配置){
控制台日志(状态);
});
};
}]);
{{headline.headline}
{{headline.usage}}
{{标题.投票}

您可以在td中绑定{{moon}}

如下更改代码:

AllControllers.controller('AppController', ['$scope', '$http', function ($scope, $http) {

    $scope.noun = "My Noun";

    $scope.headlines = [{
        headline: "Top 10 Tricks to ",
        usage: 10,
        votes: 100
    }];
}]);

<tbody ng-repeat="headline in headlines_displayed">
<tr>
    <td>{{headline.headline}} {{noun}}</td>
    <td class="center">{{headline.usage}}</td>
    <td class="center">{{headline.votes}}</td>
</tr>
</tbody>
AllControllers.controller('AppController',['$scope','$http',函数($scope,$http){
$scope.noun=“我的名词”;
$scope.headlines=[{
标题:“十大成功秘诀”,
用法:10,
票数:100
}];
}]);
{{headline.headline}{{noun}}
{{headline.usage}}
{{标题.投票}

您的$scope变量与ngRepeat变量不同。 我认为您必须更改控制器中的$scope变量:

$scope.headlines_displayed = [{
        headline: "Top 10 Tricks to "+$scope.noun,
        usage: 10,
        votes: 100
    }];

我明白了。结果是:

<td>{{headline.headline.replace("{{noun\}\}", noun)}}</td>
{{headline.headline.replace({noun\}},noun)}

在将字符串分配给作用域之前,应该使用$interpolate服务编译字符串。用{{}传递字符串将不起作用。在http成功回调中,执行以下操作。这将用范围值替换{{noon}}

 $scope.load_headlines = function() {
    $http.get('/data/headlines.json').
        success(function(data, status, headers, config){
            $scope.headlines = data;
            $interpolate($scope.headlines.headline)($scope);
        }).
        error(function(data, status, headers, config){
            console.log(status);
        });
};
}]);

看看这个

您正在谈论的Ajax代码在哪里?我认为没有必要包含Ajax调用。因为在一天结束时,$scope.headlines将是一个对象数组。属性标题将具有占位符{{noun}。因此,如何填充$scope.headlines可能与此无关。
headlines\u显示的
来自何处?如果省略
load\u headlines
方法,而只使用固定版本的
$scope.headlines
,这是否有效?很好。那是个打字错误,应该是头条新闻。不过,在呈现headline属性时,对{{noon}}没有任何更改。这是行不通的。因为我正在使用ajax调用填充$scope.headlines。$scope.headlines是动态的。我更新我的答案,你可以在html中绑定两个参数;