【发布时间】:2016-03-10 15:28:45
【问题描述】:
我有一个控制器 chatController.js 和一个 chat.html
chatController.js
$scope.getMessage = function() {
Chat.getMessage("C0RRZEDK4")
.success(function(response) {
console.log("getMessage: " + JSON.stringify(response));
$scope.channel = response.messages;
});
}
chat.html
<div ng-controller="ChatController">
<form>
<input ng-style="style(color)" type="text" ng-model="text" placeholder="text">
<button ng-click="sendMessage()">Send!</button>
<button ng-click="getMessage()">Get First Message!</button>
</form>
<li ng-repeat="msg in channel">
<li>{{msg.text}}</li>
</li>
*ng-app 在其他地方的标头中指定,sendMessage() 函数可以正常工作。
问题在于,当在 GUI 中单击 getMessage 时,ng-repeat 不会从控制器中使用 response.messages 设置的 channel 创建列表。
响应 json 如下所示:
{
"ok":true,"messages": [
{
"type":"message",
"user":"U0KTA20K0",
"text":"123",
"ts":"1457615693.000003"
},
{
"user":"U0KTA20K0",
"type":"message",
"subtype":"channel_join",
"text":"<@U0KTA20K0|julian_blair> has joined the
channel","ts":"1457615234.000002"}
],
"has_more":false
}
【问题讨论】:
-
它是我编写的一个有角度的对象,传递到 $scope 服务旁边的控制器中。
-
$scope.channel = (JSON.stringify(response)).messages
-
<div>{{channel}}</div>添加后会在您的页面上呈现什么? -
打印:
[{"type":"message","user":"U0KTA20K0","text":"123","ts":"1457615693.000003"},{"user":"U0KTA20K0","type":"message","subtype":"channel_join","text":"<@U0KTA20K0|julian_blair> has joined the channel","ts":"1457615234.000002"}]
标签: javascript html angularjs json angularjs-ng-repeat