1. 程式人生 > >angularjs http 無重新整理呼叫

angularjs http 無重新整理呼叫

1.html頁面

<body ng-app="myApp" ng-cloak>
    <form id="form1" runat="server">
        <div ng-controller="myController">
            <ul>
                <li ng-repeat="item in Users">{{item.name}}|{{item.gender}}


                </li>


            </ul>


        </div>
    </form>
</body> 

2.javascript程式碼

var myApp = angular.module("myApp", []);



    //myApp.value("BaseSource", {
    //    Users: [
    //     { "isBonusPoints": true, "name": "小芳", "likesubject": 2, "gender": 2, "integral": 100, "bonusPoints": 5 },
    //     { "isBonusPoints": true, "name": "小明", "likesubject": 1, "gender": 1, "integral": 111, "bonusPoints": 5 },
    //     { "isBonusPoints": false, "name": "小張", "likesubject": 2, "gender": 1, "integral": 98, "bonusPoints": 0 }
    //    ]
    //});
    myApp.service("Users", function () {
        this.GetUser = function ($scope, $http) {
            $http({
                method: 'get', //get/post都可以
                url: 'WebForm1.aspx?Type=1'
            }).success(function (req) {
                $scope.Users = req;
                console.log(req);
            })
        }
    });


    myApp.controller("myController", ["$scope", "$http", "BaseSource", "Users", function ($scope, $http, BaseSource, Users) {
        //$scope.Users = BaseSource.Users;
        Users.GetUser($scope, $http);

    }]);

3.後臺獲取資料

  if (Request["Type"] == "1") {
                    string ResSource =  "[{ \"isBonusPoints\": true, \"name\": \"小芳1\", \"likesubject\": 2, \"gender\": 2, \"integral\": 100, \"bonusPoints\": 5 },{ \"isBonusPoints\": true, \"name\": \"小明\", \"likesubject\": 1, \"gender\": 1, \"integral\": 111, \"bonusPoints\": 5 }, { \"isBonusPoints\": false, \"name\": \"小張\", \"likesubject\": 2, \"gender\": 1, \"integral\": 98, \"bonusPoints\": 0 } ]";
                    Response.Write(ResSource);
                    Response.End();
                }