AngularJs练习Demo9

Posted 编程猴子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AngularJs练习Demo9相关的知识,希望对你有一定的参考价值。

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Provider</title>
    <script type="text/javascript" src="~/Scripts/angular.js"></script>
    <script type="text/javascript">
        var myApp = angular.module("myApp", []);

        //
        myApp.service("serviceServices01", function ($http, $log) {//不能直接返回字符串,因为内部是用new来实例化的所以可以直接用this来表示当前的service
            //var _name = "";
            //var service = {};
            //service.setName = function (name) {
            //    _name = name;
            //}
            //service.getName = function (name) {
            //    return _name;
            //}
            //return service;
            //  $log.warn("警告");
            var _name = "";
            var service = {};
            this.setName = function (name) {
                _name = name;
            };
            this.getName = function (name) {
                return _name;
            };
            this.getData = function () {
                var myUrl = "http://wwww.phonegap100.com/apiapi.php?a=getPortallList&catid=20&page=1&callback=JSON_CALLBACK";

                return $http.jsonp(myUrl, { cache: true });//缓存请求,相同的请求直接从缓存取数据不会再跑服务器
                // return _name;
            }

        });







        myApp.controller("firstController", ["$scope", "serviceServices01", "$location", function ($scope, serviceServices01, $location) {
            $scope.name = "张三";
          console.log($location.absUrl());

            serviceServices01.getData().success(function (data) {
                console.log(data);
            }).error(function (err) {
                console.log("失败");
            });
        }]);

        myApp.controller("secondController", ["$scope", "serviceServices01", function ($scope, serviceServices01) {
            $scope.name = "李四";
        }]);

    </script>

</head>
<body>
    <div ng-app="myApp">
        <div ng-controller="firstController">

            {{name}}

        </div>
        <div ng-controller="secondController">

            {{name}}

        </div>
    </div>
</body>
</html>

  

以上是关于AngularJs练习Demo9的主要内容,如果未能解决你的问题,请参考以下文章

Python--Demo9--我最喜欢的列表

angularjs购物车练习

数据结构(C语言版) 栈和队列 算法设计Demo9

从 AngularJS url 中删除片段标识符(# 符号)

初入AngularJS基础门

AngularJS ——ngResourceRESTful APIs 使用