首页  >  文章  >  web前端  >  angularjs依赖注入

angularjs依赖注入

一个新手
一个新手原创
2017-10-11 09:40:281127浏览

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

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

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

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

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

以上是angularjs依赖注入 的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn