以下是构建链接的两种方法,其唯一目的是运行JavaScript代码.哪个更好,在功能,页面加载速度,验证目的等方面?
function myJsFunc() {
alert("myJsFunc");
}Run Code Online (Sandbox Code Playgroud)
<a href="#" onclick="myJsFunc();">Run JavaScript Code</a>Run Code Online (Sandbox Code Playgroud)
要么
function myJsFunc() {
alert("myJsFunc");
}Run Code Online (Sandbox Code Playgroud)
<a href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>Run Code Online (Sandbox Code Playgroud)
我想在浏览器遵循ui-router动态创建的链接之前验证某些条件.
我正在调查,$rootscope.$on('$stateChangeStart', ..)但我无法访问controller.$scope那里.我还需要在应用程序的几个地方使用它,并且会很麻烦.
请记住ui-sref链接到ui-sref-active(一起工作),所以我不能删除,ui-sref并且说,$state.$go('some-state')在一个名为with的函数内使用ng-click.
应该在a $scope function和a之内评估条件on-click event(在转换之前能够取消它)
我需要这样的东西:
<li ui-sref-active="active">
<a ui-sref="somestate" ui-sref-if="model.validate()">Go Somestate</a>
</li>
Run Code Online (Sandbox Code Playgroud)
我试过了:
<li ui-sref-active="active">
<a ui-sref="somestate" ng-click="$event.preventDefault()">Go Somestate</a>
</li>
<li ui-sref-active="active">
<a ui-sref="somestate" ng-click="$event.stopImmediatePropagation()">Go Somestate</a>
</li>
Run Code Online (Sandbox Code Playgroud)
和
<li ui-sref-active="active">
<a ui-sref="somestate">
<span ng-click="$event.stopPropagation();">Go Somestate</span>
</a>
</li>
Run Code Online (Sandbox Code Playgroud)
甚至
<li ui-sref-active="active">
<a ui-sref="somestate" onclick="return false;">Go Somestate</a>
</li>
Run Code Online (Sandbox Code Playgroud)
但是不起作用.
如何使用指令方法启用/禁用锚标记?
例:
JAVASCRIPT:
angular.module('ngApp', []).controller('ngCtrl',['$scope', function($scope){
$scope.create = function(){
console.log("inside create");
};
$scope.edit = function(){
console.log("inside edit");
};
$scope.delete = function(){
console.log("inside delete");
};
}]).directive('a', function() {
return {
restrict: 'E',
link: function(scope, elem, attrs) {
if(attrs.ngClick || attrs.href === '' || attrs.href === '#'){
elem.on('click', function(e){
e.preventDefault();
if(attrs.ngClick){
scope.$eval(attrs.ngClick);
}
});
}
}
};
});
Run Code Online (Sandbox Code Playgroud)