【问题标题】:ng-repeat not showing from my factory我的工厂没有显示 ng-repeat
【发布时间】:2017-01-04 18:36:41
【问题描述】:

我无法从我的工厂获取一些数据:

.factory("userDataFactory", function(){

    var user;
    var user_notes;

    var interfaz = {
      getUsuario: function(){
        return user;
      },
      getNotes: function(){
        return user_notes;
      },
      nuevoUsuario: function(userId, email){
          user = [userId, email];
      },
      addNotes: function(notes){
          user_notes = notes;
      }
    }

    return interfaz;
})

我首先要做的是使用以下代码用我的 Ruby Api 中的数据填充工厂:

$http({
            method : "POST",
            url: "http://localhost:3000/api/v1/checkuser",
            data: json
          }).then ( function mySucces(response){

              userDataFactory.nuevoUsuario(response.data.user[0].id, response.data.user[0].email);
              userDataFactory.addNotes(response.data.notes);
              $state.go('thirdday');

后来我在新控制器中调用我的工厂:

.controller('ListNotes', function($scope, userDataFactory, $state){

  $scope.notes = userDataFactory.getNotes();
  console.log($scope.notes);
  //console.log($scope.notes[0].note1);
})

最后,尝试在我的 html (listnotes.html) 中显示它

<div class="list">

    <a class="item" href="#" ng-repeat="x in notes">
        {{ x.note2 }} 

    </a>
</div>

我的路线配置以防万一:

.state('list',{
    url: '/listnotes',
    templateUrl: 'templates/listnotes.html',
    controller: 'ListNotes'
  })

那么,我错过了什么?为什么没有显示我从 API 获得的所有注释?

提前致谢

编辑

从我的 API 获取数据后,我转到“thirdday.html”视图,然后按一个按钮转到列表视图。

  .state('thirdday',{
    url: '/thirdday',
    templateUrl: 'templates/thirdday.html',
    controller: 'Notes'
  })

【问题讨论】:

  • 当你记录 $scope.notes 时控制台会说什么?如果在 元素中添加的不仅仅是 {{ x.note2 }} 会发生什么?
  • 您好,感谢您的回答。我的日志中没有显示任何内容,就像从未传递到那条线一样。我确定我的“note2”字段中有一些东西。
  • 添加日志语句以跟踪 response.data 中的内容。或者更好的是,使用调试器。

标签: angularjs ionic-framework angular2-directives


【解决方案1】:

在您的工厂中,“user_notes”包含带有键“note1”的对象(根据您的日志语句console.log($scope.notes[0].note1))。在 ng-repeat 中,您使用的是 x.note2。设为x.note1。

【讨论】:

  • 感谢您回复我。该对象有 3 个不同的音符,音符 1、音符 2 和音符 3。我刚刚尝试了他们三个。
【解决方案2】:

尝试在 html 中添加注释 {{notes}}。看看它在那里是否有价值。 可能是您只是没有在标记中添加 ng-app 或 ng-controller 或某些东西

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多