我正在尝试从addNews.html在供稿中添加新闻,但是当我单击ADDtoNews时,它会将新数据推送到数组中,但不会显示新添加的新闻
在addNews.html
Title: <input ng-model="enteredTitle" type="text"/>
ImgURL: <input ng-model="enteredUrl" type="text"/>
Description: <input ng-model="enteredDesc" type="text"/>
<a href="#!/"><button ng-click="addingNews()">ADDtoNEWS</button></a>
在index.html
newsApp.config(function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'newsFeed.html',
controller: 'NewsCtrl'
}).
when('/addnew', {
templateUrl: 'addNews.html',
controller: 'NewsCtrl'
}).
otherwise({
redirectTo: '/'
});
});
newsApp.controller('NewsCtrl', function ($scope){
$scope.newsFeed = [];
$scope.addingNews = function() {
$scope.newsFeed.push({
title: $scope.enteredTitle,
url: $scope.enteredUrl,
description: $scope.enteredDesc
});
$scope.enteredTitle = "";
$scope.enteredUrl = "";
$scope.enteredDesc = "";
console.log($scope.newsFeed.length) ;
};
}
在newsFeed.html
中<a ng-href="#!/addnew"><button type="submit">ADD</button></a>
<div ng-repeat="n in newsFeed">
<img ng-src="{{n.url}}" alt="sorry cannot load img" />
<div>
<h2>{{n.title}}</h2>
<p>{{n.description}}</p>
</div>
</div>