你可以使用一个指令,这里有一个例子:
//你的JS源
angular.directive('something', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
alert('yup');
});
}
};
}]);
//在HTML文件中
<button type="button" something>Click Me!</button>
这使您可以在您的整个项目重用你的各种代码块/功能很容易。
另一种解决方案
<!DOCTYPE html>
<html>
<head>
<title>example</title>
<script data-require="[email protected]*" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
</head>
<body ng-app="app" ng-controller="exampleController as ctrl">
<button ng-click="ctrl.name()">click me to angular</button>
<button ng-click="name()" class="btn">click me to javascript</button>
<script>
var app = angular.module("app", []);
app.controller('exampleController', [function() {
this.name = function() {
alert("angular text");
}
}]);
//JQuery Lib must be included your project
$(document).on('click', '.btn', function() {
eval($(this).attr('ng-click'));
});
function name() {
alert("javascript text");
}
</script>
</body>
</html>
不,这不是我期待answer.i问的方式,有没有办法使用Java编写的脚本代码可以在角使用JS html文件@Sa E Chowdary –