Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular无法读取属性';推动';未定义的_Javascript_Angularjs - Fatal编程技术网

Javascript Angular无法读取属性';推动';未定义的

Javascript Angular无法读取属性';推动';未定义的,javascript,angularjs,Javascript,Angularjs,我已经创建了一个简单的新闻提示器,它在我的代码笔示例中似乎工作得很好。似乎我在集成代码结构方面遇到了问题。目前,我收到以下错误 Cannot read property 'push' of undefined 这是我的控制器的代码片段 "use strict"; var app = angular.module('portlandApp'); // WEB SERVICE GET REQUEST var NewsService = function ($http){ this.$http

我已经创建了一个简单的新闻提示器,它在我的代码笔示例中似乎工作得很好。似乎我在集成代码结构方面遇到了问题。目前,我收到以下错误

Cannot read property 'push' of undefined
这是我的控制器的代码片段

"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);


 }]);

在控制器最需要的时候,声明
$scope.news=[]


当您向$scope.news添加值时,此变量未定义

您从未初始化$scope.news,只需在控制器的开头添加
$scope.news=[]
,您可以尝试

// 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();
 };

我不认为这是事实。我已经在您更新的代码段中对我的代码段进行了编辑,仍然没有初始化$scope.news我不知道这是什么情况。我已对代码段进行了编辑。在推送我的值之前,我已经定义了我的数组,使用web服务您必须先在JavaScript中定义数组并初始化“$scope.news=[]”。。。在你访问它之前。。我没有看到这样的声明。