AngularJS:如何在模板内部设置变量? [英] AngularJS: How to set a variable inside of a template?

查看:122
本文介绍了AngularJS:如何在模板内部设置变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免第三行中的{{f = ...}}语句打印出forecast[day.iso]的内容?

How can I avoid having the {{f = ...}} statement in the third line print out the content of forecast[day.iso]?

我想避免在每次迭代中都使用forecast[day.iso].temperature等.

I want to avoid using forecast[day.iso].temperature and so on for every iteration.

<div ng-repeat="day in forecast_days">
  {{$index}} - {{day.iso}} - {{day.name}}
  {{f = forecast[day.iso]}}
  Temperature: {{f.temperature}}<br>
  Humidity: {{f.humidity}}<br>
  ...
</div>

推荐答案

使用 ngInit : https://docs.angularjs.org/api/ng/directive/ngInit

<div ng-repeat="day in forecast_days" ng-init="f = forecast[day.iso]">
  {{$index}} - {{day.iso}} - {{day.name}}
  Temperature: {{f.temperature}}<br>
  Humidity: {{f.humidity}}<br>
  ...
</div>

示例: http://jsfiddle.net/coma/UV4qF/

这篇关于AngularJS:如何在模板内部设置变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆