gpt4 book ai didi

javascript - Angular Js http GET 响应显示在控制台日志中,但我无法显示 ng-view

转载 作者:行者123 更新时间:2023-11-27 23:17:00 24 4
gpt4 key购买 nike

AngularJS HTTP GET 响应显示在控制台日志中,但我无法在 div 中显示它。这是我的代码,我是初学者,我不明白为什么它不起作用,所以我在这里问。

<!DOCTYPE html>
<html lang="en">
<base href="/">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-sanitize.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
</head>
<body>
<a href="test-wallpaper">testing</a>
<div ng-app="myApp" ng-controller="newcontroller">
<div ng-bind-html="resultdata"></div>
<div ng-view></div>
<script>
var app = angular.module("myApp", ["ngRoute","ngSanitize"]);

app.config(function($routeProvider,$locationProvider){
$routeProvider
.when("/:id-wallpaper",{
templateUrl: "/new",
controller : "newcontroller"
});
$locationProvider.html5Mode(true);
});

app.controller("newcontroller", function ($scope, $http, $routeParams) {
$http({
url: "new",
method: "get",
params: { id: $routeParams.id }
}).then(function (response) {
console.log(response.data);
$scope.resultdata = response.data;
})

});
</script>
</div>
</body>
</html>

最佳答案

请删除<div ng-bind-html="resultdata"></div>从你的 DOM 中尝试创建一个 $route模板。您还可以使用 templateUrl: 'nameOfTemplate.html' 为您的模板创建一个附加文件。而不是使用 template 将其绑定(bind)为 HTML 字符串参数。请引用AngularJS routeProvider configuration .

$路由配置

app.config(function($routeProvider,$locationProvider){
$routeProvider
.when("/:id-wallpaper",{
template: '<div>{{ resultdata }}</div>',
controller : "newcontroller"
});
$locationProvider.html5Mode(true);
});

模板文件看起来像这样:

app.config(function($routeProvider,$locationProvider){
$routeProvider
.when("/:id-wallpaper",{
templateUrl: 'myFile.html',
controller : "newcontroller"
});
$locationProvider.html5Mode(true);
});

myFile.html 的内容( View )

<div>{{ resultdata }}</div>

关于javascript - Angular Js http GET 响应显示在控制台日志中,但我无法显示 ng-view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42600272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com