HTML 内容,
<button data-href="helloworld">Show href Value</button>
js内容,
$("body").on('click', 'button', function(){
console.log($(this).attr("data-href"));
});
这打印
helloworld
在控制台中。
我正在将上面的代码迁移到 angularjs,
<div ng-controller="hello">
<button data-href="helloworld">Show href Value</button>
</div>
var app = angular.module('app', []);
app.controller("hello", function(){
//someone help me with the services that are to be injected and
//and what logic goes here to print the data-href attribute value
})
有人可以帮我吗hello
控制器函数参数和内容?
如果您要切换到 AngularJS,您将不得不开始完全重新考虑您的应用程序——很难对使用选择和 DOM 操作的现有应用程序进行彻底改造。 Angular 通常用于避免这些问题。因此,你应该问自己why您需要绑定到事件而不是使用数据绑定/视图模型。
这里涉及的内容太多了,因此为了解决您眼前的问题,您可以使用指令。
<button show-href="helloworld">Show href Value</button>
// `data-` prefixes stripped from directives as part of Angular normalization
app.directive("showHref", function () {
return function (scope, elem, attr) {
elem.bind("click", function () {
console.log(attr.showHref);
});
};
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)