我有一堆有用的功能,我使用在我的网站,做各种事情的JavaScript库。
I have a javascript library with a bunch of useful functions that I make use of across my website that does various things.
我知道我无法从访问这些功能NG-点击,因为这些功能的范围之内。
I know that I cannot access these functions from an ng-click, because the functions are outside of scope.
是否有访问它们,而无需申报范围的功能,只是让库的函数的调用方式?
Is there a way to access them without having to declare a scope function that just makes a call to the function in the library?
这里是一个例子的jsfiddle。我想知道如果有一种方法,使第二个环节的工作。这完全是因为我不喜欢定义将仅仅是调用另一个函数的函数的概念。例如。
Here is a jsfiddle with an example. I would like to know if there's a way to make the second link work. This is simply because I do not like the idea of defining a function that will simply call another function. e.g.
HTML
<div ng-click="doSomething()">Click Me (Working)</div>
<div ng-click="doSomethingElse()">Click Me (Not Working)</div>
控制器JS:
$scope.doSomething = function () {
doSomethingElse();
};
外部库JS:
function doSomethingElse() {
alert("SomethingElse");
}
&LT; ------- ------- UPDATE>
感谢您的创意答复家伙!维奈K公司的答案是最简单也是最明显的,但我决定去与罗恩鄂氏的答案。原因是,我已经有了可重复使用的指令集合的全局模块,这会更容易和更清洁的我的HTML实现。还因为我有时用从库多于一个功能,然后将不得不链他们在onclick
Thanks for the creative responses guys! Vinay K's answer is the easiest and most obvious, but I decided to go with Ron E's answer. The reason is that I already have a global module with a collection of reusable directives and that would make it easier and cleaner to implement in my HTML. Also because I sometimes use more than one function from the library and then would have to chain them in the onclick:
onlick="func1(); func2(); func3();"
当一个指令就是清洁,我可以打电话给尽可能多的功能,因为我喜欢在做其他的东西为好。
Where a directive is just cleaner and I can call as many functions as I like while doing other stuff as well.
推荐答案
您可以使用一个指令,下面是一个例子:
You could use a directive, here is an example:
// in your JS source
angular.directive('something', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
alert('yup');
});
}
};
}]);
// in your HTML file
<button type="button" something>Click Me!</button>
这可以让你在整个项目pretty轻松重复使用各种code块/功能。
This allows you to reuse your various code chunks / functions across your entire project pretty easily.
相关推荐
最新文章