为什么在我的视图中看不到这个变量?
Why am I unable to see this variable in my view?
所以这是来自我的路由器的相关网段:
app.js
.state('app.browse', {
url: "/browse/:question",
controller: 'QCtrl',
resolve: {
question: function($stateParams, qService){
console.log(qService.getQuestion($stateParams.question).q);
return qService.getQuestion($stateParams.question).q;
}
},
views: {
'menuContent': {
templateUrl: "templates/browse.html"
}
}
})
/* QSERVICE HERE */
.factory('qService', function() {
var questions = [ {'q': "text text"} ];
return {
questions: questions,
getQuestion: function(index) {
return questions[index]
}
}
})
controllers.js
.controller('QCtrl', function($scope, question){
$scope.questions = qService.questions;
$scope.question = question;
})
如控制台日志所示,它准确地找到了我正在寻找的内容。
但是在我的浏览器视图中,我无法获取 question
变量!
browser.html
<ion-view view-title="Browse">
{{question}}
</ion-content>
总是显示为空!为什么会发生这种情况,我该如何解决?
Resolve 不会将问题绑定到您的控制器。
在你的控制器中执行此操作
.controller('QCtrl', function ($scope, question) {
$scope.question = question;
})
此外 - 在您的状态对象中,问题传递不正确。更正:
.state('app.browse', {
url: "/browse/:question",
resolve: {
question: function($stateParams, qService){
return qService.getQuestion($stateParams.question);
}
},
views: {
'menuContent': {
templateUrl: "templates/browse.html",
controller: 'QCtrl',
}
}
})
您的状态对象中还缺少 templateUrl。更新它以反映模板的位置,应该可以去:)
所以这是来自我的路由器的相关网段:
app.js
.state('app.browse', {
url: "/browse/:question",
controller: 'QCtrl',
resolve: {
question: function($stateParams, qService){
console.log(qService.getQuestion($stateParams.question).q);
return qService.getQuestion($stateParams.question).q;
}
},
views: {
'menuContent': {
templateUrl: "templates/browse.html"
}
}
})
/* QSERVICE HERE */
.factory('qService', function() {
var questions = [ {'q': "text text"} ];
return {
questions: questions,
getQuestion: function(index) {
return questions[index]
}
}
})
controllers.js
.controller('QCtrl', function($scope, question){
$scope.questions = qService.questions;
$scope.question = question;
})
如控制台日志所示,它准确地找到了我正在寻找的内容。
但是在我的浏览器视图中,我无法获取 question
变量!
browser.html
<ion-view view-title="Browse">
{{question}}
</ion-content>
总是显示为空!为什么会发生这种情况,我该如何解决?
Resolve 不会将问题绑定到您的控制器。
在你的控制器中执行此操作
.controller('QCtrl', function ($scope, question) {
$scope.question = question;
})
此外 - 在您的状态对象中,问题传递不正确。更正:
.state('app.browse', {
url: "/browse/:question",
resolve: {
question: function($stateParams, qService){
return qService.getQuestion($stateParams.question);
}
},
views: {
'menuContent': {
templateUrl: "templates/browse.html",
controller: 'QCtrl',
}
}
})
您的状态对象中还缺少 templateUrl。更新它以反映模板的位置,应该可以去:)