当前位置:Gxlcms > JavaScript > angularjs依赖注入

angularjs依赖注入

时间:2021-07-01 10:21:17 帮助过:11人阅读

将代码部署到线上,都会对代码做压缩。压缩会删除所有的注释、删除没有语义的空白字符、尽可能的简化变量的名称(混淆),但是数字、字符串、关键字是不会改变的。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());
    }
  }]);

以上就是angularjs依赖注入 的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行