我是 AngularJS
的新手,请帮我解决这个问题。
怎么调用jQuery
来自 AngularJS
的函数.
HTML:
<div ng-controller="TestClickController">
<button ng-click="clickFunc()">Test click</button></div>
脚本:
<script>
var app = angular.module('myApp', []);
app.controller('TestClickController', function($scope) {
$scope.clickFunc = function() {
$('#div1').html('User clicked');
};
});
</script>
单击按钮时,出现错误 "Error: $ is not defined"
.
最佳答案
您的错误意味着 jQuery 未包含/尚未加载。
如果你使用 Angular,我建议你使用 Angular 的方式来解决这个问题:
<div ng-controller="TestClickController">
<button ng-click="clickFunc()">Test click</button>
<div>
<!-- This text will be updated when the button is clicked -->
{{ testText }}
</div>
</div>
Controller :
app.controller('TestClickController', function($scope) {
$scope.testText = '';
$scope.clickFunc = function() {
$scope.testText = 'User clicked';
};
});
Angular 不成文的规则之一是:“如果你需要使用 jQuery 来做事,那么你可能做错了什么。”
关于jquery - 如何从 AngularJS 调用 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30573439/