$scopeからコントローラ名を取得する 質問する

$scopeからコントローラ名を取得する 質問する

AngularJS の現在の $scope からコントローラー名を取得する方法はありますか?

ベストアンサー1

これが良い解決策かどうかはわかりませんが、$scope.controllerNameこのテクニックを使用して注入することができました:

app.config(['$provide', function ($provide) {
    $provide.decorator('$controller', [
        '$delegate',
        function ($delegate) {
            return function(constructor, locals) {
                if (typeof constructor == "string") {
                    locals.$scope.controllerName =  constructor;
                }

                return $delegate.apply(this, [].slice.call(arguments));
            }
        }]);
}]);

それから

app.controller('SampleCtrl', ['$scope', '$log', function ($scope, $log) {
    $log.log("[" + $scope.controllerName +"] got here");
}]);

おすすめ記事