内非定向控制器功能的访问属性值 [英] Access attribute value inside non-directive controller function
问题描述
HTML内容,
<button data-href="helloworld">Show href Value</button>
内容的js,
$("body").on('click', 'button', function(){
console.log($(this).attr("data-href"));
});
这版画
helloworld
在控制台中。
我迁移高于code到angularjs,
in the console. I am migrating the above code to 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
})
有人可以帮助我与你好
控制器功能参数和内容?
Can someone help me with the hello
controller function arguments and contents?
推荐答案
如果您在切换到AngularJS你将不得不开始完全重新思考你的应用程序 - 这是很难从使用选择的现有应用进行彻底改革和DOM操作。角通常用于避免这些。因此,你应该问自己的为什么的需要绑定一个事件,而不是使用数据绑定/视图模型。
If you are switching over to AngularJS you will have to start rethinking your app entirely -- it's difficult to make a complete overhaul from an existing app that is using selection and DOM manipulation. Angular is typically used to avoid these. Thus, you should ask yourself why you need to bind to an event rather than using data bindings / the view model.
这是太多进入这里,所以为您解决眼前的问题,你可以使用一个指令。
That's too much to get into here, so to solve your immediate problem you could use a directive.
<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);
});
};
});
这篇关于内非定向控制器功能的访问属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!