var app = angular.module(‘myApp‘, [‘ng‘]); //创建一个自定义服务app.factory(‘$Debug‘, function () { return { debugSwitch:true, log: function (msg) { if(this.debugSwitch) { console.log(msg); } } }}); //推断式(猜测)/*app.controller(‘myCtrl‘, function ($scope,$Debug) { $Debug.log(‘Hello DI‘);})*/ //行内式依赖注入app.controller(‘myCtrl‘,[‘$scope‘,‘$Debug‘, function ($scope,$Debug) { $Debug.log(‘Hello Dependency Injection‘);}]);
时间: 2024-10-21 16:47:43