在页面中导航时在feed中添加新闻

时间:2019-04-14 12:07:30

标签: angularjs

我正在尝试从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> 

0 个答案:

没有答案