gpt4 book ai didi

javascript - AngularJS 显示一个 json 对象

转载 作者:行者123 更新时间:2023-11-30 12:02:58 26 4
gpt4 key购买 nike

我从我的 rest api 接收到具有指定 ID 的消息。当我显示所有新闻时,我使用了 ng-repeat 并且工作正常,但是当我想显示一个对象时,此方法不起作用。

我的.when代码:

.when('/displaynews/:id',{
templateUrl: 'views/display.html',
controller: 'NewsDisplayController',
constollerAs: 'displaydash'
})

Controller :

.controller('NewsDisplayController',
function($routeParams, NewsModel){

var displaydash = this;
var newsId = $routeParams.id;
path = 'getNewsById/'+newsId;

function getNewsById() {
NewsModel.getNewsById().then(function (result){
displaydash.news = result.data;
console.log(displaydash.news);
})
}

getNewsById();
})

console.log 的结果:

Object { id="56f1ba6b275c8aa5bf4895d8",  title="Tytul",  text="Text",  more...}

如何在我的 html 模板中显示它?

我尝试以这种方式在 html 文件中显示:

<p>{{news.title}}</p>
<p>{{news.text}}</p>

但是没有效果

最佳答案

你可以去:

angular.toJson(JSONObj);

所以,在这里你可以去:在 Controller 中:

displaydash.news = result.data;
$scope.news = angular.toJson(displaydash.news);

在 HTML 中:

<p>{{news}}</p>

您问题中的问题很简单,您正在尝试访问您尚未定义的新闻对象,尝试为其创建一个范围变量,您将能够轻松访问它:

 $scope.displaydash.news = result.data;

<p>{{displaydash.news.title}}</p>
<p>{{displaydash.news.text}}</p>

引用:https://docs.angularjs.org/api/ng/function/angular.toJson

关于javascript - AngularJS 显示一个 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36174521/

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