zoukankan      html  css  js  c++  java
  • AngularJS 依赖注入

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Demo</title>
    	<script src="js/angular-1.5.8.min.js"></script>
    </head>
    <body ng-app="myApp">
    	<h2>AngularJS简单应用</h2>
    	<div ng-controller="myCtrl">
    		<p>
    			输入一个数字:<input type="number" ng-model="number">
    			<button ng-click="square()">x<sup>2</sup></button>
    		</p>
    		<p>结果:{{result}}</p>
    	</div>
    	<script>
    		var app = angular.module('myApp',[]);
    		//创建value对象,传递数值5
    		app.value("inputValue",5);
    
    		app.factory('facService',function () {
    			var factory = {};
    			factory.multiply = function (a, b) {
    				return a * b;
    			}
    			return factory;
    		});
    
    		app.config(function ($provide) {//在应用启动前修改模块配置
    			$provide.provider('proService',function () {
    				this.$get = function () {//通过$get函数返回内容
    					var factory = {};
    					factory.multiply = function (a, b) {
    						return a * b;
    					}
    					return factory;
    				}
    			});
    		});
    
    		app.provider('proService',function () {
    			this.$get = function () {//通过$get函数返回内容
    				var factory = {};
    				factory.multiply = function (a, b) {
    					return a * b;
    				}
    				return factory;
    			}
    		});
    
    		app.service('serService',function (facService) {
    			this.square = function (a) {
    				return facService.multiply(a,a);
    			}
    		});
    
    		//angular中三种声明依赖的方式
    		/*app.controller('myCtrl',function ($scope,serService,inputValue) {
    			$scope.number = inputValue;
    			$scope.result = serService.square($scope.number);
    			$scope.square = function () {
    				$scope.result = serService.square($scope.number);
    			}
    		});*/
    
    		/*app.controller('myCtrl',['$scope','serService','inputValue',function ($scope,serService,inputValue) {//顺序不能乱
    			$scope.number = inputValue;
    			$scope.result = serService.square($scope.number);
    			$scope.square = function () {
    				$scope.result = serService.square($scope.number);
    			}
    		}]);*/
    
    		var MathFn = function ($scope,serService,inputValue) {
    			$scope.number = inputValue;
    			$scope.result = serService.square($scope.number);
    			$scope.square = function () {
    				$scope.result = serService.square($scope.number);
    			}
    		} 
    		MathFn.$inject = ['$scope','serService','inputValue'];
    		app.controller('myCtrl',MathFn);
    	</script>
    </body>
    </html>
    

      

  • 相关阅读:
    [百度百科]PCI-E的速度
    Oracle 高版本往低版本备份恢复的方法
    PHP-Java-Bridge使用笔记
    Delphi获取其它进程窗口句柄的3种方法
    二层交换机当三层交换机,使用单臂路由实现二层交换机上的VLAN互通
    Python下科学计算包numpy和SciPy的安装【原创】
    OpenGL: 你不知道的左右手坐标系
    Delphi XE7的安卓程序如何调用JAVA的JAR,使用JAVA的类?
    科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系
    phpmyadmin #1045 #2002 无法登录 MySQL 服务器的解决方
  • 原文地址:https://www.cnblogs.com/handsomehan/p/AngularJS.html
Copyright © 2011-2022 走看看