Home >Web Front-end >JS Tutorial >angularjs dependency injection

angularjs dependency injection

一个新手
一个新手Original
2017-10-11 09:40:281215browse

When the code is deployed online, the code will be compressed. Compression will delete all comments, delete meaningless whitespace characters, and simplify variable names as much as possible (obfuscation), but numbers, strings, and keywords will not change. There are three types of angularjs dependency injection: marked dependency injection, inline dependency injection and inference (guessing). Officially recommended inline dependency injection
The following example uses inline dependency injection
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 code

:
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());
    }
  }]);

The above is the detailed content of angularjs dependency injection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn