【发布时间】:2015-07-24 11:29:21
【问题描述】:
我尝试在ng-click 的模板中设置范围变量,然后在控制器中读取它。示例:
HTML:
<section id="editCtrl" data-ng-controller="EditCtrl as edit">
{{ edit.myVar = []; "" }}
Click the following buttons:<br>
<span data-ng-click="edit.myVar['entry'] = 'test'">Click me</span>
<span data-ng-click="edit.showMyVar();">Show MyVar in console</span>
</section>
JS:
// This controller is just for the example - I would never add a controller variable within the template
var app = angular.module("app", []);
app.controller("EditCtrl", function(){
var edit = this;
edit.showMyVar = function(){
console.log(edit.myVar);
};
});
但是变量“edit.myVar”一直是一个空数组。我究竟做错了什么?在我的解释中,这是一个有效的 ng-click 表达式。
现场示例:JSFiddle
【问题讨论】:
标签: javascript angularjs templates controller