
AngularJS
html<!DOCTYPE html><html ng-app="taskApp"><head> <script src="https://Ajax.Googleapis.com/Ajax/libs/AngularJS/1.8.2/angular.min.JS"></script></head><body ng-controller="TaskController"> <h2>任务列表</h2> <ul> <li ng-repeat="task in tasks">{{ task }}</li> </ul> <input type="text" ng-model="newTask" placeholder="添加新任务"> <button ng-click="addTask()">添加任务</button> <script> var app = angular.module('taskApp', []); app.controller('TaskController', function($scope) { $scope.tasks = ['任务1', '任务2', '任务3']; $scope.addTask = function() { $scope.tasks.push($scope.newTask); $scope.newTask = ''; }; }); </script></body></html>传统Ajax:html<!DOCTYPE html><html><head> <script src="https://code.jquery.com/jquery-3.6.4.min.JS"></script></head><body> <h2>任务列表</h2> <ul id="taskList"></ul> <input type="text" id="newTask" placeholder="添加新任务"> <button onclick="addTask()">添加任务</button> <script> var tasks = ['任务1', '任务2', '任务3']; function displayTasks() { $('#taskList').empty(); tasks.forEach(function(task) { $('#taskList').append('<li>' + task + '</li>'); }); } function addTask() { var newTask = $('#newTask').val(); tasks.push(newTask); displayTasks(); $('#newTask').val(''); } $(document).ready(function() { displayTasks(); }); </script></body></html> 4. 通过以上比较可以看出,AngularJS的响应式调用相较于传统的Ajax调用更加简洁和便捷。双向数据绑定减少了手动操作DOM的需求,提高了代码的可维护性和可读性。然而,开发者仍需根据项目的具体需求选择合适的方案,因为每种方式都有其适用的场景。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号