angularjs - 如何纠正以下代码的解析错误
问题描述
html代码是:
<div ng-controller="myController">
<input type="input" ng-model="title" />
<br/>
<p class="jm-find" title1="title" title2="spell('fe')"></p>
</div>
Javascript代码:
var app = angular.module('app', []);
app.controller("myController", function ($scope) {
$scope.title = "binding";
$scope.spell = function(ele){
console.log("me "+ele);
}
});
app.directive("jmFind", function () {
return {
replace: true,
restrict: 'C',
transclue: true,
scope: {
title1: "=",
title2: "&"
},
template: "<div><button ng-click={{title2}}>{{title1}}</button></div>"
};
});
我试图了解 Angularjs 自定义指令中的“ & ”用法。我遵循了几个示例,但缺少调用该spell
函数的正确语法。
JSFiddle 链接:http: //jsfiddle.net/k03ozLts/
解决方案
调用使用表达式 ( &
) 绑定的 AngularJS 表达式:
app.directive("jmFind", function () {
return {
scope: {
title1: "=",
title2: "&"
},
̶t̶e̶m̶p̶l̶a̶t̶e̶:̶ ̶"̶<̶d̶i̶v̶>̶<̶b̶u̶t̶t̶o̶n̶ ̶n̶g̶-̶c̶l̶i̶c̶k̶=̶{̶{̶t̶i̶t̶l̶e̶2̶}̶}̶>̶{̶{̶t̶i̶t̶l̶e̶1̶}̶}̶<̶/̶b̶u̶t̶t̶o̶n̶>̶<̶/̶d̶i̶v̶>̶"̶
template: "<div><button ng-click='title2()'>{{title1}}</button></div>"
};
});
推荐阅读
- python - 将嵌套的 Python OrderedDict 转换为 QJSValue
- firebase - 仅从 Firestore 导出原始数据
- javascript - AjaxData DataList - 在每个单元格的底部对齐一个按钮
- ckeditor - ckEditor - 我们可以禁用子子列表吗?
- php - php网站显示文件而不是下载
- flutter - The instance member 'barberData' can't be accessed in an initializer
- javascript - 绝对定位在 React Native 中不起作用 - Android
- javascript - 测试赛普拉斯后如何恢复数据库
- flutter - 使用文档中的字段进行另一个查询
- nosql - 为什么 couchdb 寻找“_users”数据库?