我是AngularJS的新手,我正在尝试使用它构建一个网站。我的问题是,由于某种原因,我不能在成功回调中使用forEach循环推送到数组。我将发布我的输入代码,以便您可以看到并告诉我我做错了什么。
(function(angular,undefined){
'use strict';
var contentBuilder = angular.module('contentBuilder', []);
contentBuilder.value('fileURL','include/_data/navigation.js');
contentBuilder.factory('dataShare',["fileURL",'$http',function(fileURL,$http){
return function()
{
var file = $http.get(fileURL).success(
function(response) {
return response.data;
});
return file;
};
}]);
// This is for the Menu
contentBuilder.controller('MenuController', ['$scope','dataShare',function ($scope,dataShare) {
$scope.menu_contents = [];
dataShare()
.success(function(data) {
$scope.menu_contents = data;
});
}]);
// all content
contentBuilder.controller('ContentController', ['$scope','$http','dataShare',function($scope,$http,dataShare)
{
$scope.pagecontent = [];
var pagesLabel = [];
//$scope.sectionHeader = sectionName;
dataShare().success(function(data) {
angular.forEach(data, function(value, key) {
this.push(value);
}, pagesLabel);
});
console.log(pagesLabel);
$scope.init = function(url, sectionName)
{
$http.get('include/_data/'+url)
.success(function(data) {
var section = (data[sectionName] != undefined)? data[sectionName] : data;
angular.forEach(section, function(value, key) {
var allSections = (isNaN(key) )? value[0] : value;
$scope.pagecontent.push(allSections);
});
}).
error(function(data) {
console.log("error "+data);
});
};
}]);})(window.angular);
我非常感谢任何帮助,因为我真的需要继续讨论这个问题。
由于