1

I have created a simple news ticker which seems to work fine on my codepen example. It seems i having a problem with integration - code structure. As it stands I am receiving the following error.

Cannot read property 'push' of undefined

Here is my a code snippet of my controller.

"use strict";

 var app = angular.module('portlandApp');

// WEB SERVICE GET REQUEST
var NewsService = function ($http){
this.$http = $http;
}

NewsService.prototype.getarticles = function () {
return this.$http.get('app/data/news.json').then(function (response){
    return response.data;
});
  };

app.service("newsService", NewsService);

angular
 .module('portlandApp')
 .controller('newsCtrl', ['$scope', 'newsService', '$location', '$interval',   '$timeout', function($scope, newsService, $location, $timeout, $interval) {

 var promise = newsService.getarticles();
  promise.then(function (data){
  $scope.articles = data.news.map(function(item) {
    //item.date = moment().format('Do MMMM YYYY');
    return item;
  });
console.log(data)
 });
 // AMOUNT OF ARTICLES
   $scope.articleLimit = 4;

// NEWS TICKER FUNCTION
$scope.moving = false;

$scope.moveLeft = function() {
    $scope.moving = true;
    $timeout($scope.switchFirst, 1000);
};
$scope.switchFirst = function () {
    $scope.news.push($scope.newsLink.shift());
    $scope.moving = false;
    $scope.$apply();
};

$interval($scope.moveLeft, 2000);


 }]);
1
  • Initialise '$scope.news = []' first... before you access it.. I dont see anywhere such declaration. Commented Aug 26, 2016 at 10:23

3 Answers 3

2

You never intialize $scope.news, just add $scope.news = [] at the beginning of the controller

Sign up to request clarification or add additional context in comments.

2 Comments

I don't this that's the case . I have made a edit to my code snippet
In your updated snippet, there still not initialization of $scope.news
0

You can try

// NEWS TICKER FUNCTION
$scope.moving = false;


$scope.news = []  // need to define array before push value.

enter code here

$scope.moveLeft = function() {
   $scope.moving = true;
   $timeout($scope.switchFirst, 1000);
 };
$scope.switchFirst = function () {
   $scope.news.push($scope.newsLink.shift());
   $scope.moving = false;
   $scope.$apply();
 };

2 Comments

I don't this that's the case . I have made a edit to my code snippet. I have already defined my array before pushing my value, using a web service
you have to define array in JavaScript also
0

At the very beggining of your controller, declare that $scope.news= []

When you add values to your $scope.news, this variable is undefined

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.