数组是否必需?AngularJs依赖注入差异
app.controller('myController', ['$scope', function($scope){
}])
这个工作是否像上面的代码?
app.controller('myController', function($scope){
})
数组是否必需?AngularJs依赖注入差异
app.controller('myController', ['$scope', function($scope){
}])
这个工作是否像上面的代码?
app.controller('myController', function($scope){
})
该数组始终是必需的,但它不必组合到控制器定义中。有一个更干净的方式来做到这一点,它是我的首选方法。我所做的是以功能性的方式创建控制器。例如:
app.controller('myController', MyController);
//dependency injection done here using the array of definitions
MyController.$inject = ['$scope'];
//all of the dependencies are added as parameters into the controller function
function MyController ($scope) {
//insert controller code
}
正如Sajeetharan在答案中指出的那样,这被称为显式方法。您应该查看获得Angular 1团队认可的this style guide其他模式,这些模式有助于使代码更清洁,更易于维护
是的,它会以同样的方式工作,它就是difference.You需要使用explicit
依赖注入(第二种方法)。
即使缩小它将$ scope转换为变量a并将$ http转换为变量b,它们的标识仍保留在字符串中。
谢谢!这只是一个简单的问题,因为我从来没有使用显式依赖注入 –