首页 > 网站 > WEB开发 > 正文

AngularJS~集成的ajax和服务的注入

2024-04-27 14:06:06
字体:
来源:转载
供稿:网友

AngularJS~集成的Ajax和服务的注入

AngularJS很美,以至于迷倒了不少年青人和我这位大叔,它的美不仅仅是在写法上,而且在设计方法上都进乎于完美,用什么服务就注入什么服务,这样方法本来就很直观,程序员感觉直观了,程序在运行起来也按需要装载,这种按需要装载无论在性能上还是在表现力上都远远优于完全加载方式。

Ajax加载数据

$http服务提供了一组ajax的方法,加载数据,Get,Post都有支持,而$http服务在angular里就是被动态装载的,在面向对象里叫做DI或者IOC

       angular.module('todoApp', []).controller('RealDataController', function ($http, $scope) {            var self = this;            $http.get('/SOA/GetMenus').success(function (data) {                self.dataList = data;            }).error(function (data, status, headers, config) { });

上面是一种装载$http服务,并使用http服务的一种方法,而下面这种是通过别名的方法使用它,代码更加精简

     angular.module('todoApp',[]).controller('RealDataController2', ["$http", "$scope", function ($h, $s) {            var self = this;            $h.get('/SOA/GetMenus').success(function (data) {                self.dataList = data;            }).error(function (data, status, headers, config) { });        }]);

通过上面的程序我们就可以把数据绑定到HTML元素上了

  <div ng-controller="RealDataController as real">        <ul>            <li ng-repeat="item in real.dataList">                <span>{{item.MenuID}}</span>                <span>{{item.MenuName}}</span>                <span>{{item.UpdateDate | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>                <span><a href="javascript:void(0)" ng-click="real.edit()">编辑</a></span>|                <span><a href="Javascript:void(0)" ng-click="real.del(item)">删除</a></span>            </li>        </ul>    </div>

结果如下

PRAFZAAAgAElEQVR4nO1dPVIsvQ6dBRASsAQyZjGkk7MAdkA0q2ALVLEEAgJCbs4CqHoEhAT9AvgGW9I5lt0/4x50iro13W35p69bx7JlazMEAoFAINANNseuQCAQCAQCvwhaCgQCgUBHCFoKBAKBQEcIWgoEAoFARwhaGjabeAmBQGDYbP5HLovpl8DDfv9vlow9bVmsvaGRf2lpY+G4dQsEAotBq12uiDWNib/pq1hPS6I+qHrppSfNrAi1m9ESehS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2xV6ae4ZYrpEEWdPq3SR7n1TS0j0NNwOMNq99itDN074HEgJwiezPmoHSMCW/iXwWpbOjlOVCcGAoHA6WG2eEtdIWgpEAgEAh0haCkQCPxdmI7gk/9F0+pqPkemgUAgEAi0IWgpEAgEAh0haKnaF67Bd84UOVkfvEBgnfA4pFUlmB6zuTx42hIO4otCM4TYfoLSy20qOL1OUJTiOQSrBQLTAu3XcaZfYt2laTttWh9UPeE+XkwzK/60aiOkkip9QksoW/PSzNPPYUXB7tHHJkpfHO/AWNS95+P3DaSXCc007P4Zi2UPH9I3l2GmiVSbEUhg3UDa30MGhNI055E8OY2tj5Z62ETZdND9yfXupeDfudxB3yC7Tc0EmrFWSku68uiO/j0TJlFt+JiT7lG0bEbSUlsCnWz9k3i/w2F++NC8dbAH8HSobvRudhAPfCabDc4lwkUZj8ExQvoUJBXNwq48fRt2GEBels9k6shUIjZBkXW6ncRLfzTQUu3c5nhMoNoedrvdGg9q/Q8mA23AmXgNtGQyigmSj66kpyYdIR8OJ4d9/ttf1Q1qfkKZ1wMEeiAKejB7d2oDiKOzVZTdNDQhVCgkQ6aGkqSqGnmF94JGbCn6NsThuun5gLgs2TiAY5tKpt1TnMRbclLrF8vGW0qfmq9oREtoncdm8LDbPazy/HAnSWiO8eRcK2ImOyFaksPhvMOQsAgGWpVYXbylpCwa2CKTFRmJo8GRQiEZFsgMEGJO3

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表