无法绑定数据以查看 angularjs

Can't bind data to view angularjs

我在离子框架中使用 angularjs 将我的数据绑定到我的视图时遇到了一些问题。

这是我的controller.js:

.controller('InventoryCtrl', function($scope, Category, Inventory) {
$scope.categories = Category;


$scope.searchInventory = function(category){
var word = category.Category;

var ref = new Firebase('https://vivid-heat-2430.firebaseio.com/');

var invenRef = ref.child('Inventories');
var prodRef = invenRef.child('Products');


invenRef.orderByChild("Products").on("child_added", function(snapshot) {
  var data = snapshot.val();
  var store = data.Inventory;
  var prod = data.Products;
  var test = Object.keys(prod);    

  for( var i = 0; i < test.length; i++){
    if (test[i] == word) {
      console.log(test[i]);
      console.log("Storage place: " + data.Inventory + " Datum inventaris: " + data.Date);
      $scope.show= test[i];

    };

  };

});

};
})

在这里,我正在从我的阵列中获取我的产品。那我比较一下。它会给我匹配的对象,还会给我它所属的存储位置。我可以将其记录在我的控制台中。但是当我试图将它绑定到我的视图时。它什么也没给我。

这是我的index.html:

<ion-content>
    <p><b>Zoek categorie</b></p>
  <ion-scroll direction="y" >
    <div style="max-height: 300px" ng-controller="InventoryCtrl">
      <ion-list>
        <ion-radio ng-repeat="category in categories" class="item-accordion" ng-model="checked.check" value="{{category.Category}}" ng-click="searchInventory(category)">
          <p>{{category.Category}}</p>
        </ion-radio>
        <br>
      </ion-list>
    </div>

    <div>
        <ion-list>
            <ion-item>
              {{show}}                  
            </ion-item>
        </ion-list>
      </div>
  </ion-scroll>
  </ion-content>

而且我不明白我做错了什么......是因为我的$scope.show还在.on方法里面吗?

嘿,我猜你的 Category 依赖是一个被注入到你的控制器中的服务?在那种情况下,我通常会写这样的东西。

.controller('InventoryCtrl', function($scope, Category, Inventory) {
    $scope.categories = [];
    Category.query(funciton(categories){
        $scope.categories = angular.copy(categories);
    });

这将在您在控制器中查询服务后存储您的类别。之后,只需在你的 html 中使用类似 NG-REPEAT 的东西。

<li ng-repeat='cat in categories'>
    {{cat.name}}
    {{cat.id}}
</li>

不太熟悉 Firebase,但我假设它是您用来提取数据的第 3 方服务。您的数据更新可能发生在 angular 摘要周期之外。将数据附加到范围后,只需添加此内容即可。

$scope.digest()

@YerkenTussupekov 关于第 3 方服务和摘要周期是正确的。

That's why at Firebase we created AngularFire.

AngularFire 是一个 Angular 模块,它提供一组用于同步收集和身份验证的服务。

angular.module('app', ['firebase']) // include it in the dep array
  .constant('FirebaseUrl', '<my-firebase-app>') // https://vivid-heat-2430.firebaseio.com/ in your case
  .service('rootRef', ['FirebaseUrl', Firebase)
  .factory('categoryArray', CategoryArray)
  .controller('MyCtrl', MyController);

function CategoryArray(rootRef, $firebaseArray) {
  var invenRef = rootRef.child('Inventories');
  return $firebaseArray(invenRef);
}

function MyController($scope, categoryArray) {
  $scope.category = categoryArray;
}