像 ng-repeat 一样更改变量名称但不重复?

Change name of variable like in ng-repeat but without repeating?

在我的应用程序中,说 $scope.item。在我的页面中,我有将其称为其他内容的模板,例如 $scope.product.

在一个页面上,我需要将模型引用为 item,但随后使用 ng-include 打开将其引用为 product 的模板。

当数组中只有一项时,我做了 ng-repeat="product in item"。现在我用一个对象取消了单个数组,它只是一个对象。如果我做 ng-repeat 它会重复对象中的每个 key/value 对。不是我想要的。

有没有办法说 ng-alias="item as product" 之类的?

ngInit 用于别名。

ng-init="item = product"

根据 documentation

The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.

如果是一次性绑定,您可以这样做

<... ng-init="product = item" ...>

然而,据我了解,您出于别名目的对 ng-repeat 进行黑客攻击,而无需重复。在这种情况下,ng-init 仍然是一个黑客,以后会给您带来麻烦。

做你想做的事情的正确方法是将你的模板放入一个指令中,然后link你的指令的product变量来自给定的项目。即 html 看起来像这样:

<my-directive product="item"></my-directive>