我有$ 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