如何从AngularJS调用jQuery

Jac*_*ack 1 jquery angularjs

我是新来的AngularJS,请帮我解决这个问题.我怎样才能调用该jQuery函数AngularJS.

HTML:

<div ng-controller="TestClickController">
<button ng-click="clickFunc()">Test click</button></div>
Run Code Online (Sandbox Code Playgroud)

脚本:

<script>
var app = angular.module('myApp', []);
app.controller('TestClickController', function($scope) {
    $scope.clickFunc = function() {
        $('#div1').html('User clicked');
    };
});
</script>
Run Code Online (Sandbox Code Playgroud)

单击该按钮时,我收到错误"Error: $ is not defined".

dev*_*qon 9

您的错误意味着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>
Run Code Online (Sandbox Code Playgroud)

控制器:

app.controller('TestClickController', function($scope) {
    $scope.testText = '';
    $scope.clickFunc = function() {
        $scope.testText = 'User clicked';
    };
});
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

一个不成文的角度规则是:" 如果你需要使用jQuery来做事情,那么你可能会做错事. "