为什么我无法在我的视图中看到这个变量

Why am I unable to see this variable in my view?

本文关键字:变量 视图 我的 为什么      更新时间:2023-09-26

这是我路由器的相关段:

应用.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]
    }
  }
})

控制器.js

.controller('QCtrl', function($scope, question){
  $scope.questions = qService.questions;
  $scope.question = question;
})

它准确地找到了我正在寻找的内容,如控制台日志所示。

但是在我的浏览器视图中,我无法获取question变量!

浏览器.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',
      }
    }
  })

状态对象中还缺少模板 URL。更新它以反映模板的位置,应该很好:)