码迷,mamicode.com
首页 > 其他好文 > 详细

Angular service

时间:2014-08-28 21:02:26      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   for   div   cti   

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="lib/angular.js"></script>
</head>
<body>
    <div ng-controller="ServiceCtrl">
        <label for="username">
            Type in a Github username
        </label>
        <input type="text" ng-model="username" placeholder="Enter a Github username">
        <ul>
            <li ng-repeat="event in events">
                {{event.actor.login}} {{event.repo.name}}
            </li>
        </ul>
    </div>
    <script>
        angular.module(myApp.services, [])
        .factory(githubService, function ($http) {
            var githubUrl = https://api.github.com;

            var runUserRequest = function (username, path) {
                return $http({
                    method: JSONP,
                    url: githubUrl + /users/ + username + / + path + ?callback=JSON_CALLBACK
                });
            }
            return {
                events: function (username) {
                    return runUserRequest(username, events);
                }
            }
        });

        angular.module(myApp, [myApp.services])
        .controller(ServiceCtrl, function ($scope, githubService) {
            $scope.$watch(username, function (newUsername) {
                githubService.events(newUsername).success(function (data, status, headers) {
                    $scope.events = data.data;
                })
            });
        });
    </script>
</body>
</html>

 

Angular service

标签:style   blog   http   color   io   ar   for   div   cti   

原文地址:http://www.cnblogs.com/wangwenfei/p/angularjs12.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!