【问题标题】:How can I disable blink ng-repeat of AngularJS?如何禁用 AngularJS 的闪烁 ng-repeat?
【发布时间】:2014-03-08 06:28:59
【问题描述】:

在收到 AngularJS 提交的查询后,我试图显示一些重复的对象。 当我只看到以下页面而没有任何提交查询时,ng-repeat 将呈现一个 ng-repeat 的 div 部分而没有结果(因此为空)。为什么会发生这种情况,如何禁用显示空结果渲染?

  • html

代码

<form ng-submit="doSearch()" ng-keyup="doSearch()" name="myForm">
  <input type="text" placeholder="<%= t('.placeholder') %>" ng-model="query" required>
  <input type="submit" class='button postfix' value="<%= t('.register') %>" ng-disabled="myForm.$invalid">
</form>

<div ng-repeat="result in results">
  <div class="row">
    <div>
      <p ng-bind-html="result.contentSnippet"></p>
    </div>

    <div>
      <a href="/#" target="_blank">link</a>
    </div>
  </div>
</div>
  • 控制器

代码

google.load "feeds", "1"

kindlers = angular.module("kindlers", [ "ngSanitize" ])

findFeeds = ($scope)->
  google.feeds.findFeeds $scope.query, (result) ->
    console.log $scope.query   
    if $scope.query == "undefined" || result.error
      $scope.results = undefined
    else
      $scope.results = result.entries 
    $scope.$apply()

@mainCtrl = ($scope, $http) ->
  console.log($scope.results)
  $scope.doSearch = ->
    google.setOnLoadCallback findFeeds($scope)
  • JavaScript(从上面的 CoffeeScript 转换而来)

代码如下。控制台.log($scope.results);显示“未定义”。

(function() {
  var findFeeds, kindlers;

  google.load("feeds", "1");

  kindlers = angular.module("kindlers", ["ngSanitize"]);

  findFeeds = function($scope) {
    return google.feeds.findFeeds($scope.query, function(result) {
      console.log($scope.query);
      if ($scope.query === "undefined" || result.error) {
        $scope.results = void 0;
      } else {
        $scope.results = result.entries;
      }
      return $scope.$apply();
    });
  };

  this.mainCtrl = function($scope, $http) {
    console.log($scope.results);
    return $scope.doSearch = function() {
      return google.setOnLoadCallback(findFeeds($scope));
    };
  };

}).call(this);

【问题讨论】:

  • 你能显示你的控制器代码吗?
  • @Oledje,上面是控制器的代码。
  • 您不应该将 $results 对象设置为 undefined。将其初始化为空数组,当没有结果时,将其设置回空数组... $scope.results = [];

标签: javascript angularjs coffeescript


【解决方案1】:

确保在您的控制器中结果完全为空。你可以这样初始化它:$scope.results = [];

【讨论】:

  • @gipcompany 您的控制器可以从父控制器继承此值 ($scope.results)。
  • @emilioicai, $scope.results 就在 mainCtrl 显示未定义之后。
  • @Oledje,在这种情况下我应该重命名结果吗?我不认为这个控制器只存在一个,所以它不会从任何控制器继承任何值。
  • 在初始化时尝试将此属性的值设置为空数组('[]')。
【解决方案2】:

ng-show="result.length > 0' 怎么样

【讨论】:

    【解决方案3】:

    $scope.results 初始化为{},并在出错时将其默认为{},而不是未定义。

    google.load "feeds", "1"
    
    kindlers = angular.module("kindlers", [ "ngSanitize" ])
    
    findFeeds = ($scope)->
      google.feeds.findFeeds $scope.query, (result) ->
        console.log $scope.query   
        if $scope.query == "undefined" || result.error
          $scope.results = {}
        else
          $scope.results = result.entries 
        $scope.$apply()
    
    @mainCtrl = ($scope, $http) ->
      $scope.results = {}
      console.log($scope.results)
      $scope.doSearch = ->
        google.setOnLoadCallback findFeeds($scope)
    

    【讨论】:

    • 这不是必须的,看这个例子,当列表为空时,带有ng-repeat指令的div不显示:plnkr.co/edit/P11PSafTahTciBvbuwHW?p=preview
    • @Alexander,我添加了 ng-show="results" 但得到了相同的结果。
    • 谢谢philippd,很高兴知道。我改变了我的答案@gipcompany
    猜你喜欢
    • 2014-07-31
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2017-12-22
    • 1970-01-01
    • 2013-11-23
    • 2017-12-23
    相关资源
    最近更新 更多