我有$ scope.question其中有問題的所有頁面。角js:解決依賴路線
我想循環頁面明智的問題。爲此我寫了一個函數questionsCtrl。這個函數我在配置路由時調用。
但在這裏我得到了undefined。
請建議如何從$ scope.questions獲取頁面的數據。
app.js
(function() {
"use strict";
var app = angular.module("autoQuote",["ui.router","ngResource"]);
app.config(["$stateProvider","$urlRouterProvider", function($stateProvider,$urlRouterProvider){
$urlRouterProvider.otherwise("/");
$stateProvider
.state("step1", {
url : "",
controller: "questionsCtrl",
resolve: {
pageQuestion: $scope.questions
}
})
.state("step2", {
url : "/step2",
controller: "questionsCtrl",
})
}]
);
}());
questionCtrl.js
(function() {
"use strict";
angular
.module("autoQuote")
.controller("questionsCtrl",["$scope","$state","$q",questionsCtrl]);
function questionsCtrl($scope,$state,$q) {
console.log('here in get question for page: '+$state.current.name);
//var deferred = $q.defer();
if($state.current.name == "" || $state.current.name == "step1")
{
$scope.pageQuestion = $scope.RC1_Cars_v2;
}
else if($state.current.name == "step2")
{
$scope.pageQuestion = $scope.RC1_Drivers_v2;
}
//return deferred.promise;
console.log($scope);
}
}());
HTML
<div class="container-fluid col-md-8">
<div ng-controller="autoQuoteCtrl">
<div ng-repeat="que in pageQuestion">
<!-- pagequestion will be in loop here -->
</div>
</div>
</div>
這裏是我完整的代碼plunker http://plnkr.co/edit/kLc6DPqzQgLNpUBaLtZi?p=preview。
感謝您的詳細解答。我在plunker中更新了app.js和questionResource.js。但得到錯誤。 –
事實上,你的Plunker中有很多錯誤。你如何從頭開始創建一個新的Plunker,並且只添加那些你真正需要的東西來實現你的解決方案,這意味着一個服務來獲取你的數據,一個路由器配置,模板和一個控制器。 – LordTribual