使用AngularJS在Ionic中显示控制器信息。 ¿调用控制器时?

时间:2015-09-13 15:45:44

标签: angularjs ionic

我是Ionic Framework的初学者,我发现了一个我无法找到解决方案的问题。嗯,这是我的代码的摘录:

我在view.html中有这段代码

  <ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="nota in notas" >
      <h1>{{nota.titulo}}</h1>
      <h2>{{nota.texto}}</h2>
    </ion-item>

这是我的控制器:

.controller('ChatsCtrl', function($scope, Chats, $cordovaSQLite) {
  $scope.notas= Chats.getNotas();
})

在services.js中,函数getNotas()是这样的:

.factory('Chats', function( $cordovaSQLite) {

  return {

    getNotas: function() {
    ...
  }
 }
}

好吧,当我进入显示来自DB的信息的应用程序屏幕时,它运行良好,但我不明白为什么最后一条记录没有显示。但是,当我为页面F5充电时(在浏览器)记录出现。我知道在调用视图进行渲染时会调用控制器,但是,如果我在其他视图中设置了一个新注释,为什么当我尝试在其他视图中看到这个最后一条记录时这不显示? 我认为这个问题应该是愚蠢的,但我不明白为什么会这样。 该应用程序是一个简单的项目,设置和从SQLite数据库获取笔记,我认为这将更容易。 非常感谢你。

//Controlador DasControl
.controller('DashCtrl', function($scope, $cordovaSQLite) {

    $scope.insert = function(titulo, texto) {
      if (window.cordova) {
        db = $cordovaSQLite.openDB({ name: "my.db" }); //device
      }else{
        db = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100); // browser
      }
        var query = "INSERT INTO people (title, text) VALUES (?,?)";
        $cordovaSQLite.execute(db, query, [titulo, texto]).then(function(res) {
            console.log("INSERT ID -> " + res.insertId);
        }, function (err) {
            console.error(err);
        });                      
    }



})

//Controlador para la vista de notas
.controller('ChatsCtrl', function($scope, Chats, $cordovaSQLite) {
  $scope.notas= Chats.getNotas();
})

当我调用函数时插入工作正常,但是当我调用第二个控制器时,只有在我重新加载页面时才能正常工作。

.factory('Chats', function( $cordovaSQLite) {

  return {

    getNotas: function() {

      console.log("peticion notas");

      //Consulta en la base de datos
      if (window.cordova) {
        db = $cordovaSQLite.openDB({ name: "my.db" }); //device
      }else{
        db = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100); // browser
      }
          //Creamos un vector donde vamos a introducir las notas.
          var notas = [];
          var salida = null;
          var query = "SELECT * FROM people";
          var res = $cordovaSQLite.execute(db, query).then(function(res) {
              var len = res.rows.length;
              if(len>0) {
              for (var i = 0; i < len; i++) {
                notas.push({
                  titulo: res.rows.item(i).title,
                  texto: res.rows.item(i).text
                });
                console.log("SELECTED -> " + res.rows.item(i).title + " " + res.rows.item(i).text);
              }
            }
              if(res.rows.length > 0) {
                 console.log("SELECTED -> " + res.rows.item(0).title + " " + res.rows.item(0).text);
              } else {
                  console.log("No results found");
              }
          }, function (err) {
              console.error(err);
          });
          return notas;
    }

0 个答案:

没有答案
相关问题