如何使用模型中的函数表达式将函数动态绑定到ng-click
How to dynamically bind function to ng-click using the function expression from model
我的Angular模板中有以下元素来生成一个带有Font Awesome图标的按钮栏:
<li
data-ng-repeat="btn in navigation.buttonBar"
data-ng-click="{{ btn[1] }}"
class="btn btn-default" style="font-size: 30px; vertical-align: middle;"
tooltip-placement="bottom" tooltip="{{ btn[2] }}">
<i class="fa {{ btn[0] }}"></i>
</li>
navigation.buttonBar是以下数组:
[
[ "fa-minus", "less()", "Show fewer cloud buttons." ],
[ "fa-plus", "more()", "Show more cloud buttons." ],
[ "fa-minus-square", "smaller()", "Make cloud buttons smaller." ],
[ "fa-plus-square", "bigger()", "Make cloud buttons bigger." ],
[ "fa-bars", "toggleShowStrings()", "Toggle display of matching strings." ],
[ "fa-refresh", "initialize();", "Load more content." ],
[ "fa-undo", "resetQuery()", "Clear query." ]
]
文本和图标渲染正确,但生成的按钮不起作用。当我检查该元素时,我发现btn[1]
已正确展开。用什么替换{{ btn[1] }}
以使其正常工作?
我不认为您可以将函数表达式从绑定中指定为字符串。如果你试图使用插值,它会抛出语法错误,如果你把它用作绑定,它也没有用。相反,您可能需要使用$parse
并执行类似操作。
app.controller('MainCtrl',['$scope', '$parse', function($scope, $parse) { //<-- inject parse
//.. Some code
//Make this as handler to ngClick passing expression which is function name you have in your model
$scope.callFunc = function(exp){
$parse(exp)($scope); //Parse the function name to get the expression and invoke it on the scope
}
//Your specific function to be called
$scope.less = function(){
console.log('less');
}
$scope.more = function(){
console.log('more');
}
在你看来:-
data-ng-click="callFunc(btn[1])"
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $parse) {
$scope.navigation = {buttonBar:[
[ "fa-minus", "less()", "Show fewer cloud buttons." ],
[ "fa-plus", "more()", "Show more cloud buttons." ],
[ "fa-minus-square", "smaller()", "Make cloud buttons smaller." ],
[ "fa-plus-square", "bigger()", "Make cloud buttons bigger." ],
[ "fa-bars", "toggleShowStrings()", "Toggle display of matching strings." ],
[ "fa-refresh", "initialize();", "Load more content." ],
[ "fa-undo", "resetQuery()", "Clear query." ]
]};
$scope.callFunc = function(exp){
$parse(exp)($scope);
}
$scope.less = function(){
console.log('less');
}
$scope.more = function(){
console.log('more');
}
});
<link data-require="bootstrap-css@*" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.25/angular.js" data-semver="1.2.25"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<i>Click the first 2 buttons and check the console</i>
<ul><li
data-ng-repeat="btn in navigation.buttonBar"
data-ng-click="callFunc(btn[1])"
class="btn btn-default" style="font-size: 30px; vertical-align: middle;"
tooltip-placement="bottom" tooltip="{{ btn[2] }}"
><i class="fa {{ btn[0] }}"></i></li></ul>
</div>
相关文章:
- 将值动态绑定到jquery中的切换按钮
- 注册OpenLayers事件时,即使使用匿名函数或绑定,JavaScript关闭也会触发循环内的所有内容
- 如何使用AnguarJS动态绑定网站数据
- 在extJS 4.2中,有没有一种方法可以将模型值动态绑定到表单
- 为什么prototypjs观察到回调函数有绑定
- 是否可以使用Sinon.JS检查函数参数绑定是否正确
- 我想在ext.js4.2中将树存储动态绑定到树面板中
- 选择与给定函数没有绑定的所有元素
- 如何让 AngularJS 选取在 JavaScript 中创建的动态绑定
- Emberjs - 将参数动态绑定到视图
- 将函数事件绑定到更改函数的复选框/标签
- KnockoutJS删除动态绑定
- jquery验证,动态绑定表单验证
- 如何将变量与函数动态绑定
- jQuery动态绑定文件输入并在函数中检索文件
- 在jQuery上动态绑定函数
- 如何为每个函数动态绑定“this”
- 如何使用模型中的函数表达式将函数动态绑定到ng-click
- jQuery点击函数未绑定到动态更改的类
- 多个元素上的动态绑定处理程序将只调用最后一个绑定函数