Đây là chỉ thị dựa trên another question hỗ trợ liên kết ng-href.
Chỉ
'use strict';
var myApp = angular.module('myApp', [
'ngAnimate'
]);
/**
* @ngdoc directive
* @name myMobileApp.directive:stopEvent
* @description Allow normal ng-href links in a list where each list element itselve has an ng-click attached.
*/
angular.module('myApp')
.directive('stopEvent', function($location, $rootScope) {
return {
restrict: 'A',
link: function(scope, element) {
element.bind('click', function(event) {
// other ng-click handlers shouldn't be triggered
event.stopPropagation(event);
if(element && element[0] && element[0].href && element[0].pathname) {
// don't normaly open links as it would create a reload.
event.preventDefault(event);
$rootScope.$apply(function() {
$location.path(element[0].pathname);
});
}
});
}
};
})
.controller('TestCtrl', ['$rootScope', '$scope', 'Profile', '$location', '$http', '$log',
function($rootScope, $scope, Profile, $location, $http, $log) {
$scope.profiles = [{'a':1,'b':2},{'a':3,'b':3}];
$scope.goToURL = function(path, $event) {
$event.stopPropagation($event);
$location.path(path);
};
}
]);
<div ng-repeat="x in profiles"
ng-click="goToURL('/profiles/' + x.a, $event)">
<a stop-event ng-href="/profiles/{{x.b}}">{{x}}</a>
</div>
Nguồn
2014-12-10 12:57:45
Xem thêm http://stackoverflow.com/questions/14544741/angularjs-directive-to-stoppropagation nơi một chỉ thị được xác định để ngăn chặn tuyên truyền. –