【发布时间】:2017-12-04 14:17:36
【问题描述】:
我仍然对 AngularJS 有所了解,所以我的问题可能存在缺陷,但是否可以延迟控制器的执行,直到加载 json 文件(在不同的函数中)?
这是控制器:
app.controller('progressCtrl', ['$scope', '$routeParams', '$http', 'content', function($scope, $routeParams, $http, content) {
function initScope() {
$scope.pageId = $routeParams.pageId;
$scope.totalPages = 0;
content.success(function(data) {
$scope.pages = data;
angular.forEach($scope.pages, function(value, key) {
if ($scope.totalPages <= value.id) $scope.totalPages = value.id;
if ($scope.pageId == value.id) {
value.class = "active";
} else {
value.class = "";
}
if (incompleteObjectives.length>0) {
var matchFound = false;
for (var i=0;i<incompleteObjectives.length-1;i++) {
if (incompleteObjectives[i].indexOf('page-'+value.id+'-')) {
matchFound = true;
}
}
if (!matchFound) {
value.class += " completed";
}
} else {
value.class += " completed";
}
});
});
}
initScope();
}]);
这里是调用'getData'函数的工厂,这是加载json文件的地方......
/*Factory that runs http.get on content.json, used by multiple controllers */
app.factory('content', ['$http', function($http) {
var url = 'json/content.js';
return $http.get(url)
.success(function(data) {
for (var i = 0; i < data.length; i++) {
numberOfPages += 1;
}
// load page .js files
var tempData = getData(0);
for (var i = 1; i < numberOfPages; i++) {
(function(i) {
tempData = tempData.then(function() {
return getData(i);
});
}(i));
}
return data;
})
.error(function(data) {
return data;
});
}]);
// loads individual page data, stores in pageData
function getData(id) {
return $.ajax({
url: 'json/page' + (id + 1) + '.js',
dataType: 'json'
}).done(function(d) {
if(firstRun)
addPageObjectives(id, d)
// push data into pageData array, for use later when pages are routed to
console.log("pushing page data into pageData for page "+id);
pageData.push(d);
}).fail(function() {
// console.log('ERROR loading page index ' + id);
});
}
为了完整起见,这里是 angular html,它具有 ng-class:
<div ng-if="content.titletext!=undefined" class="sidetitle" ng-controller="progressCtrl">
<div class="container">
<div>
{{content.titletext}}
</div>
<div class="progress-buttons">
<div ng-repeat="(skey,page) in pages" class="button {{skey+1}}" ng-class="page.class" ng-attr-id="{{'nav-'+(skey+1)}}">
<div ng-switch="page.titletext || '_undefined_'">
<span ng-switch-when="_undefined_">{{skey+1}}</span>
</div>
</div>
</div>
</div>
<div class="container">
<div ng-if="content.chapterTitleText!=undefined" class="side-chapter-title">
{{content.chapterTitleText}}
</div>
<div class="progress-pages">
<span>{{pageId}} of {{totalPages}}</span>
</div>
</div>
</div>
最终它是我想要访问的 pageData 数组,但此时进度控制器正在运行,只加载了第一个 json 文件,所以我只能从第一页访问详细信息。我希望在加载所有 json 文件时运行 progressController 中的代码 - 并且仍然可以访问 value.class 属性。
这可能吗?
谢谢。
更新
从下面的答案中,这就是我尝试过的。我知道以下内容是错误的,但如果可能的话,我想要一些关于错误的提示。
/*Factory that runs http.get on content.json, used by multiple controllers */
app.factory('content', ['$http', function($http) {
var promise;
var url = 'json/content.js';
function init() {
console.log("Good news! content factory init function does get called!");
promise = $http.get(url).then(function success(data) {
for (var i = 0; i < data.length; i++) {
numberOfPages += 1;
}
console.log("data.length = "+data.length); // this outputs as 'undefined'; I'm not sure why, given loading was, presumably, successful
// load page .js files
var tempData = getData(0);
for (var i = 1; i < numberOfPages; i++) {
(function(i) {
tempData = tempData.then(function() {
return getData(i);
});
}(i));
}
return data;
}); // I took off the .error lines because they threw an error - I'm assuming the syntax didn't match the new 'success' syntax above
}
init(); // I added this - not sure if that's right, but otherwise the init function didn't run...
function getData(id) {
return $.ajax({
url: 'json/page' + (id + 1) + '.js',
dataType: 'json'
}).done(function(d) {
if(firstRun)
addPageObjectives(id, d)
// push data into pageData array, for use later when pages are routed to
console.log("pushing page data into pageData for page "+id);
pageData.push(d);
}).fail(function() {
// console.log('ERROR loading page index ' + id);
});
return promise;
}
// public API
return {
getData: getData
}
}]);
在其他地方,在控制器中我使用了行,例如:
content.getData().then(function(data) {
....
}
...访问页面数据(我认为这不起作用)。我猜这个更新在很多地方都是错误的,但如果人们愿意提供帮助,那就太好了。再次感谢。
【问题讨论】:
-
(使用
$http和$.ajax只是粗鲁) -
不出所料,我根本不知道你的意思。我认为这是某种角度的笑话。如果是这样 - 好一个!我得到它! (说清楚,我不明白)。
-
我认为他们说你同时使用这两种方法而不是只选择一种方法很奇怪
-
我们有一个团队正在为此工作,而我刚刚回到这个项目(这是一种有效的躲闪方式吗?)。我还是一个角度新手,所以我不明白为什么使用这两种方法很重要。如果有人对(我认为)更大的问题有答案,我很想听听。
-
@moosefetcher 使用 angularjs' $http 将触发范围摘要
标签: javascript html angularjs json