@{ 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>
时间: 2024-10-25 08:45:26