您好,欢迎来到刀刀网。
搜索
您的当前位置:首页angularjs依赖注入

angularjs依赖注入

来源:刀刀网


将代码部署到线上,都会对代码做压缩。压缩会删除所有的注释、删除没有语义的空白字符、尽可能的简化变量的名称(混淆),但是数字、字符串、关键字是不会改变的。angularjs依赖注入有3种分别是标记式依赖注入和行内式依赖注入和推断式(猜测)。官方推荐行内式依赖注入
如下例采用行内式依赖注入
html

<!DOCTYPE html><html ng-app="myApp"><head lang="en">
 <meta charset="UTF-8">
 <title></title>
 <script src="js/angular.js"></script>
 <script src="js/demo13.min.js"></script></head><body><p ng-controller="myCtrl">
 <button ng-click="handleClick()">
 clickMe </button></p></body></html>

js代码

:
var app = angular.module('myApp', ['ng']);

app.factory('$student', function () { return {
 checkScore: function () { return 80;
 }
 }
})

//推断式依赖注入
/*app.controller('myCtrl', function ($scope, $student) {

 $scope.handleClick = function () {
 $student.checkScore();
 }
 });*/

//行内式依赖注入
app.controller('myCtrl',
 ["$scope", "$student", function ($scope, $student) {
 $scope.handleClick = function () {
 console.log($student.checkScore());
 }
 }]);

Copyright © 2019- gamedaodao.com 版权所有 湘ICP备2022005869号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务