javascript angular.equals() 是否可以用作角度表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26932715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:42:06  来源:igfitidea点击:

Does angular.equals() work as an angular expressions?

javascriptangularjsobjectequality

提问by David says Reinstate Monica

I'm trying to display a div if an object is non-empty. Using thisanswer, Im trying to use angular.equalsto check emptyness, but its not behaving as expected

如果对象非空,我会尝试显示一个 div。使用这个答案,我试图用来angular.equals检查空虚,但它的行为不符合预期

var test = angular.module('test',[]);

test.controller('testCtrl', ['$scope', function($scope){
  $scope.foo={};
  $scope.bar="bam"
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test">
  <div ng-controller="testCtrl">
    <div ng-show="!angular.equals(foo,{})">{{bar}}</div>
  </div>
</div>

The expectation here is that the value of barwill only show if foois not equal to an empty object. However, foois clearly set to {}and yet barstill shows.

这里的期望是,bar如果foo不等于一个空对象,则值才会显示。但是,foo显然设置为{}并且bar仍然显示。

回答by Philipp Gayret

If you want to access the angularobject from templates or expressions, you have to make it available on the scope of where you want to use it. In this case you can put it on the testCtrl's scope.

如果angular要从模板或表达式访问对象,则必须使其在要使用它的范围内可用。在这种情况下,您可以将其放在testCtrl的范围内。

var test = angular.module('test',[]);

test.controller('testCtrl', ['$scope', function($scope){
  $scope.angular = angular;
  $scope.foo={};
  $scope.bar="bam"
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test">
  <div ng-controller="testCtrl">
    <div ng-show="!angular.equals(foo,{})">{{bar}}</div>
  </div>
</div>

I generally put utility objects on $rootScope, so they're available from everywhere.

我通常将实用程序对象放在 上$rootScope,因此它们随处可用。

回答by Bennie Krijger

A cleaner way would be to only add the angular equals method to the $scope:

一种更简洁的方法是只将 angular equals 方法添加到 $scope 中:

var test = angular.module('test',[]);

test.controller('testCtrl', ['$scope', function($scope){
   $scope.angularEquals = angular.equals;
}

Then you can use the equals method in the template, like:

然后你可以在模板中使用 equals 方法,比如:

<div ng-show="!angularEquals(foo,{})">{{bar}}</div>

回答by Blazemonger

Your view is looking for a function on the scope, and $scope.angular.equalsdoes not exist. You need to write one like this:

您的视图正在寻找作用域上的函数,但该函数$scope.angular.equals不存在。你需要写一个这样的:

var test = angular.module('test', []);

test.controller('testCtrl', ['$scope',
  function($scope) {
    $scope.foo = {};
    $scope.bar = "bam";
    $scope.isEmpty = function(obj) {
      return angular.equals(obj,{});
    };
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test">
  <div ng-controller="testCtrl">
    <div ng-hide="isEmpty(foo)">{{bar}}</div>
  </div>
</div>

回答by Viktor

Angular functions can't be used inline, AFAIK. You could do the equal check with a function inside the controller instead and return the result.

Angular 函数不能内联使用,AFAIK。您可以使用控制器内部的函数进行相等检查并返回结果。