使用指令控制器进行角度缩小?

Rol*_*ndo 7 javascript minify angularjs angularjs-directive angularjs-controller

如果我有以下内容:

myapp.directive('directivename', ...

    return {
        ...
        restrict: 'E',
        controller: MyController,
        ...
    }

    function MyController($scope, $somethingelse) {
        // Contents of controller here
    }
);
Run Code Online (Sandbox Code Playgroud)

如何修改它以便MyController在缩小时不会被破坏?我收到以下错误:

错误:[$ injector:unpr]未知提供者:eProvider < - e

PSL*_*PSL 20

可以使用显式依赖项注释来解决它.你有什么隐含的注释,在缩小时会导致问题.您也可以使用$inject或内联数组注释来注释指令中的依赖项.

MyController.$inject = ['$scope', '$somethingelse'];

function MyController($scope, $somethingelse) {
    // Contents of controller here
}
Run Code Online (Sandbox Code Playgroud)

或者在指令中:

return {
    ...
    restrict: 'E',
    controller: ['$scope', '$somethingelse', MyController],
    ...
}
Run Code Online (Sandbox Code Playgroud)

或者使用.controller语法注册控制器

app.controller('MyController', ['$scope', '$somethingelse', MyController]);
Run Code Online (Sandbox Code Playgroud)

并在指令中设置控制器名称而不是构造函数.

return {
    ...
    restrict: 'E',
    controller: 'MyController',
    ...
}
Run Code Online (Sandbox Code Playgroud)

您还可以查看ng-annotate,您不需要使用显式注释.