html5_依赖注入的5个重要的组件
2017-10-16 18:37
211 查看
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="angular-1.3.0.js"></script> <script> // 依赖注入:5个重要的组件factory、provider、service、value、constant var app = angular.module("myapp", []); //第一种factory // app.factory("calculator", function () { // return { // add: function (a, b) { // return a + b; // }, // // subtract: function (a, b) { // return a - b; // }, // // multiply: function (a, b) { // return a * b; // }, // // divide: function (a, b) { // if (b != 0) { // return a / b; // } // return 0; // } // }; // }); //第二种provider // app.provider("calculator",function(){ // this.$get = function(){ // return { // add: function (a, b) { // return a + b; // }, // // subtract: function (a, b) { // return a - b; // }, // // multiply: function (a, b) { // return a * b; // }, // // divide: function (a, b) { // if (b != 0) { // return a / b; // } // return 0; // } // } // } // }); //第三种service 单例 app.service("calculator",function(){ this.add = function (a, b) { return a + b; }; this.subtract = function (a, b) { return a - b; }; this.multiply = function (a, b) { return a * b; }; this.divide = function (a, b) { if (b != 0) { return a / b; } return 0; } }); //value // app.value("a", "除数不能等于零!"); // app.value("a", "除数不能等于零!!!!!"); // app.controller("myCtrl", function ($scope, a) { // //输出的是 "除数不能等于零!!!!!" 下面覆盖了 // console.log(a); // }); //constant app.constant("a", "除数不能等于零!"); app.constant("a", "除数不能等于零!!!!!"); app.controller("myCtrl", function ($scope, a) { //输出的是 "除数不能等于零!" 下面没有覆盖 console.log(a); }); app.controller("myCtrl", function ($scope, calculator) { //赋值 calculator是方法的第一个参数 $scope.result1 = calculator.add(8, 4); $scope.result2 = calculator.subtract(8, 4); $scope.result3 = calculator.multiply(8, 4); $scope.result4 = calculator.divide(8, 4); }); </script> </head> <body ng-app="myapp"> <div ng-controller="myCtrl"> <div>8 + 4 = {{ result1 }}</div> <div>8 - 4 = {{ result2 }}</div> <div>8 * 4 = {{ result3 }}</div> <div>8 / 4 = {{ result4 }}</div> </div> </body> </html>
相关文章推荐
- 用spring的注解建立bean与bean之间的关系组件装配及自动注入依赖对象
- 在MVC3项目中结合NInject组件实现依赖注入的设计
- 使用spring注解方式实现组件的声明,并且用注解注入方式解决依赖关系
- 工具类如何使用依赖注入其他组件
- 在MVC3项目中结合NInject组件实现依赖注入的设计
- 基于ABP模块组件与依赖注入组件的项目插件开发
- AngularJS依赖注入 value值 注入组件
- AngularJS依赖注入 constant 常量注入组件
- Spring依赖注入,自动扫描组件,自动装配,注解注入
- html5 5个重要特性
- Angular 4依赖注入学习教程之组件服务注入(二)
- 依赖注入及企业库Unity组件学习笔记
- 在MVC3项目中结合NInject组件实现依赖注入的设计
- Spring 注入集合、依赖检查、自动组件扫描、过滤组件自动扫描、自动装配属性
- Angular之依赖注入(injector)与原生View组件
- 【Unity】微软的一款依赖注入组件
- Guice 依赖注入
- (Spring框架)单例和多例以及依赖注入
- 依赖注入和控制反转
- Android单元测试(五):依赖注入,将mock方便的用起来