<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> </head> <body ng-app="myApp"> <div ng-controller="firstController"> {{name}} {{age}} </div> <div ng-controller="secondController"> {{name}} {{age}} </div> <script> var m1 = angular.module('myApp',[]); m1.service('serviceServices01',function($http,$rootScope,$log,$filter){//请求不放在Controller,放在service里面,service层, $rootScope.age='50'; $log.warn('jinggao'); var _name=''; this.setName=function(name){ _name=name; }; this.getName=function(){ return _name; }; this.getData=function(){ var myUrl ="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSON_CALLBACK"; return $http.jsonp(myUrl,{cache:true}); }; }); // console.log(m1); m1.controller('firstController',['$scope','serviceServices01',function($scope,serviceServices01){ console.log(serviceServices01); serviceServices01.getData().success(function(data){ console.log(data); }).error(function(){ alert('shibai '); }); $scope.name='张三'; }]); m1.controller('secondController',['$scope','serviceServices01',function($scope,serviceServices01){ console.log(serviceServices01); $scope.name='李四'; }]); </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <script src="angular.min.js"></script> </head> <body ng-app="myApp"> <div ng-controller="firstController"> {{name}} {{age}} </div> <div ng-controller="secondController"> {{name}} {{age}} <div id="div1" style="height:400px; background:red;"> </div> <div id="div2" style="height:400px; background:yellow;"> </div> <div id="div3" style="height:400px; background:blue;"> </div> <div id="div4" style="height:400px; background:red;"> </div> </div> <script> var m1 = angular.module('myApp',[]); m1.service('serviceServices01',function($http,$rootScope,$log,$filter){ $rootScope.age='50'; $log.warn('jinggao'); var _name=''; this.setName=function(name){ _name=name; }; this.getName=function(){ return _name; }; this.getData=function(){ var myUrl ="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSON_CALLBACK"; return $http.jsonp(myUrl,{cache:true}); }; }); // console.log(m1); m1.controller('firstController',['$scope','serviceServices01',function($scope,serviceServices01){ //console.log(serviceServices01); /* * serviceServices01.getData().success(function(data){ console.log(data); }).error(function(){ alert('shibai '); }); $scope.name='张三'; * */ }]); m1.controller('secondController',['$scope','serviceServices01','$location','$anchorScroll',function($scope,serviceServices01,$location,$anchorScroll){ //console.log(serviceServices01); $scope.name='李四'; console.log($location.absUrl()) ; $location.hash('hello');//location用于路由管理, $location.path('/new'); $location.path('/new').replace(); $location.search({'age':20}); $location.hash('div3'); }]); </script> </body> </html>
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> #parent div{ width:300px; height:500px; border:1px #000 solid; margin:20px;} #parent ul{ width:200px; position:fixed; top:0; right:0;} </style> <script src="angular.min.js"></script> <script> var m1 = angular.module('myApp',[]); m1.controller('FirstControl',['$scope','$location','$anchorScroll',function($scope,$location,$anchorScroll){ $scope.changeDiv=function(div){ $location.hash(div); } }]); </script> </head> <body> <div id="parent" ng-controller="FirstControl"> 点击1就跳到1div,点击2就跳到2div, <ul> <li ng-repeat="id in [1,2,3,4,5]" ng-click="changeDiv('div'+id)">{{id}}aaaaaaaaaa</li> </ul> <div ng-repeat="id in [1,2,3,4,5]" ng-attr-id="div{{id}}">{{id}}</div> </div> </body> </html>
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/7240802.html,如需转载请自行联系原作者