我有一种情况,有时我将本地人传递到控制器中,有时没有。
我想做的是,如果“locals”不可用,不抛出异常,而是调用控制器并让 locals == null。
.controller('SomeCtrl', ['$scope', 'locals', function ($scope, locals) {
if (!locals) // do something
else // do something else
};
在一种情况下,控制器是使用 $controller 服务创建的。$controller('SomeCtrl', { locals: '一些本地人');
但在其他情况下,我无法控制控制器的实例化方式,并且本地人不可用。我希望 Angular 能像普通的 javascript 一样通过 undefined ...
这不酷:
Error: Unknown provider: localsProvider <- locals
at Error (<anonymous>)
at http://localhost:1573/Scripts/angular.js:2832:15
at Object.getService [as get] (http://localhost:1573/Scripts/angular.js:2960:39)
at http://localhost:1573/Scripts/angular.js:2837:45
at getService (http://localhost:1573/Scripts/angular.js:2960:39)
at invoke (http://localhost:1573/Scripts/angular.js:2978:13)
at Object.instantiate (http://localhost:1573/Scripts/angular.js:3012:23)
at $get (http://localhost:1573/Scripts/angular.js:4981:24)
at http://localhost:1573/Scripts/angular.js:4560:17
at forEach (http://localhost:1573/Scripts/angular.js:137:20)