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

angularjs dependency injection

一个新手
Release: 2017-10-11 09:40:28
Original
1240 people have browsed it

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>
Copy after login

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());
    }
  }]);
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template