如何禁用AngularJS的闪烁重复?

时间:2014-03-07 16:22:08

标签: javascript angularjs coffeescript

我试图在收到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转换而来)

代码如下。的console.log($ scope.results);显示&#34; undefined&#34;。

(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);

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

ng-show =“result.length&gt; 0'

怎么样?

答案 2 :(得分:0)

$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)