我无法决定在以下情况下使用哪种方法。我试图在点击按钮时发出警报。我可以使用 2 种方法来做到这一点。哪个是最佳做法,请告诉我为什么?
方法一
<div ng-app="app">
<button alert>directive</button>
</div>
var app = angular.module('app', ['ngRoute']);
app
.directive('alert', function(){
return {
link: function(scope, element, attr) {
element.on('click', function(){
alert('clicked');
})
}
}
})
方法二
<div ng-app="app" ng-controller="MainCtrl">
<button ng-click="go()">ng-click</button>
</div>
app.controller('MainCtrl', ['$scope', function($scope) {
$scope.go = function() {
alert('clicked');
}
}]);
谢谢, 乳山
最佳答案
让我用例子向你解释一下。
HTML
<div ng-app="myapp">
<div ng-controller="MyCtrl1">
<button ng-click="showAlert('hello')">Fist</button>
<button ng-click="showConsole('hello')">for Fist one only</button>
<button show-alert="first using directive">Fist with directive</button>
</div>
<div ng-controller="MyCtrl2">
<button ng-click="showAlert('hello second')">Second</button>
<button show-alert="first using directive">Second With directive</button>
</div>
<div ng-controller="MyCtrl3">
<button ng-click="showAlert('hello third')">Third</button>
<button show-alert="third using directive">third with directive</button>
</div>
</div>
JS
var myApp = angular.module('myapp',[]);
myApp
.controller('MyCtrl1', function ($scope) {
$scope.showAlert = function (msg) {
alert(msg);
};
$scope.showConsole = function (msg) {
console.log(msg);
};
})
.controller('MyCtrl2', function ($scope) {
$scope.showAlert = function (msg) {
alert(msg);
};
})
.controller('MyCtrl3', function ($scope) {
$scope.showAlert = function (msg) {
alert(msg);
};
})
.directive('showAlert', function(){
return{
restrict: 'A',
link: function(scope, ele, attr){
var eventName = attr.evetName || 'click';
var mas = attr.showAlert || 'just alert';
ele.on(eventName, function(){
alert(mas);
});
}
};
});
如您在示例中所见,与在每个 Controller 中直接使用 $scope.showAlert 相比,show-alert="[MSG]" 能够减少代码复制.所以在这种情况下创建指令更好。
但是,如果 $scope.showConsole 只使用了一次,我们不会在任何地方重复使用它。所以直接在 Controller 内部使用它很好。
虽然。您还可以为 showConsole 功能创建指令,如果您觉得将来它也会在其他地方使用。完全没问题。这个决定完全取决于您的用例。
关于javascript - Angularjs - ng-click 函数与指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30184999/
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
我需要一个通过输入字符串进行计算的方法,像这样function="(a/b)*100"a=25b=50function.something>>50有什么方法吗? 最佳答案 您可以使用instance_eval:function="(a/b)*100"a=25.0b=50instance_evalfunction#=>50.0请注意,使用eval本质上是不安全的,尤其是当您使用外部输入时,因为它可能包含注入(inject)的恶意代码。另请注意,a设置为25.0而不是25,因为如果它是整数a/b将导致0(整数)。
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法
在这段Ruby代码中:ModuleMClassC当我尝试运行时出现“'M:Module'的未定义方法'helper'”错误c=M::C.new("world")c.work但直接从另一个类调用M::helper("world")工作正常。类不能调用在定义它们的同一模块中定义的模块函数吗?除了将类移出模块外,还有其他解决方法吗? 最佳答案 为了调用M::helper,你需要将它定义为defself.helper;结束为了进行比较,请查看以下修改后的代码段中的helper和helper2moduleMclassC
也许这听起来很荒谬,但我想知道这对Ruby是否可行?基本上我有一个功能...defadda,bc=a+breturncend我希望能够将“+”或其他运算符(例如“-”)传递给函数,这样它就类似于...defsuma,b,operatorc=aoperatorbreturncend这可能吗? 最佳答案 两种可能性:以方法/算子名作为符号:defsuma,b,operatora.send(operator,b)endsum42,23,:+或者更通用的解决方案:采取一个block:defsuma,byielda,bendsum42,23,