在标记中设置角度范围变量 [英] Set angular scope variable in markup

查看:22
本文介绍了在标记中设置角度范围变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题:如何在 html 中设置范围值以供控制器读取?

var app = angular.module('app', []);app.controller('MyController', function($scope) {控制台日志($scope.myVar);});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><div ng-app='app'><div ng-controller="MyController" app-myVar="test">{{myVar}}

JSFiddle:http://jsfiddle.net/ncapito/YdQcX/

解决方案

ng-init 在循环内分配变量时不起作用.用{{myVariable=whatever;""}}

尾随的 "" 停止对任何文本求值的 Angular 表达式.

然后你可以简单地调用 {{myVariable}} 来输出你的变量值.

我发现这在迭代多个嵌套数组时非常有用,我想将当前迭代信息保存在一个变量中,而不是多次查询.

Simple question: How can I set a scope value in html, to be read by my controller?

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

app.controller('MyController', function($scope) {
  console.log($scope.myVar);
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
  <div ng-controller="MyController" app-myVar="test">
    {{myVar}}
  </div>
</div>

JSFiddle: http://jsfiddle.net/ncapito/YdQcX/

解决方案

ng-init does not work when you are assigning variables inside loop. Use {{myVariable=whatever;""}}

The trailing "" stops the Angular expression being evaluated to any text.

Then you can simply call {{myVariable}} to output your variable value.

I found this very useful when iterating multiple nested arrays and I wanted to keep my current iteration info in one variable instead of querying it multiple times.

这篇关于在标记中设置角度范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆