我有一個解決方案。試試你的plnkr。
注意我是如何硬編碼的$scope.notifications
。你會想要檢索實際的數據 - 不知道如何在plnkr中做到這一點。當你檢索JSON的時候,你將不得不信任這樣的數據:
var app = angular.module('notifications', []);
app.controller('mainController', function($scope, $http, $sce) {
$http({
method: 'GET',
url: 'notifications.json'
}).success(function(data){
console.log('success');
$scope.notifications = data;
for (var i=0; i<$scope.notifications.length; i++){
$scope.notifications[i].userAction = $sce.trustAsHtml($scope.notifications[i].userAction)
}
}).error(function(data){
console.log('error');
});
$scope.myLimit = 3;
$scope.loadmore = true;
});
編輯,因爲也許解釋是應該的。以下是我所做的更改:
- Angular Module發生錯誤(名稱錯誤),所以我更改了JS的第一行。
$scope.notifications
必須在JS中聲明。
- 新增$ scope.myLimit所以我們可以修改這個變量
limitTo
- 在NG-點擊,除去
notifications = !notifications
,加入myLimit = notifications.length
所以它可以顯示所有結果。
- 最後,添加了ng-bind-html而不是
{{notification.userAction}}
,因此它可以顯示爲HTML。
JS:
var app = angular.module('notifications', []);
app.controller('mainController', function($scope, $http) {
$http({
method: 'GET',
url: 'notifications.json'
}).success(function(){
console.log('success');
}).error(function(){
console.log('error');
});
$scope.notifications = [
{
"avatar" : "images/otherUser.png",
"type" : "",
"userName" : "Ivana Stankova",
"userAction" : "<span class=\"heart\">❤</span> your photo",
"targetObject" : "images/targetPhoto.jpg"
},
{
"avatar" : "images/otherUser2.png",
"type" : "",
"userName" : "Ivana Stankova",
"userAction" : "<span class=\"heart\">❤</span> your photo",
"targetObject" : "images/targetPhoto.jpg"
},
{
"avatar" : "images/otherUser4.png",
"type" : "checkedIn",
"userName" : "Dave Peters",
"userAction" : "Checked in<br/><a href=\"#\">962 Grant Street Victoria</a>",
"targetObject" : "images/place.jpg"
},
{
"avatar" : "images/otherUser4.png",
"type" : "commented",
"userName" : "Dave Peters",
"userAction" : "Commented on <a href=\"#\">your post</a><p>Hey guys, 8 o’clock? Let’s get some food first? How<br/>about that fancy restaurant we wanted to try for...</p>",
"targetObject" : "images/targetPhoto.jpg"
},
{
"avatar" : "images/otherUser.png",
"type" : "",
"userName" : "Ivana Stankova",
"userAction" : "<span class=\"heart\">❤</span> your photo",
"targetObject" : "images/targetPhoto.jpg"
},
{
"avatar" : "images/otherUser.png",
"type" : "",
"userName" : "Ivana Stankova",
"userAction" : "<span class=\"heart\">❤</span> your photo",
"targetObject" : "images/targetPhoto.jpg"
},
{
"avatar" : "images/otherUser4.png",
"type" : "",
"userName" : "Dave Peters",
"userAction" : "<a href=\"#\">Made a new post.</a>",
"targetObject" : "images/targetPhoto.jpg"
},
{
"avatar" : "images/otherUser.png",
"type" : "",
"userName" : "Ivana Stankova",
"userAction" : "Started following you.",
"targetObject" : ""
},
{
"avatar" : "images/fivePeople.png",
"type" : "",
"userName" : "",
"userAction" : "Five people Started following You.",
"targetObject" : ""
}
]
$scope.myLimit = 3;
$scope.loadmore = true;
});
的Index.html
你是否完成了在angularjs網站上的教程?它很好地演示瞭如何做到這一切,包括如何使用ngAnimate。 –