Home > Article > Web Front-end > angularjs dependency injection
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('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()); } }]);
The above is the detailed content of angularjs dependency injection. For more information, please follow other related articles on the PHP Chinese website!