How can I use a registered controller in my angular directive?

后端 未结 2 1273
没有蜡笔的小新
没有蜡笔的小新 2021-01-11 11:56

I have a controller registered like this:

myModule.controller(\'MyController\', function ($scope, ...some dependencies...)
{
    ....

Using

2条回答
  •  萌比男神i
    2021-01-11 12:29

    The answer you've already accepted works, and in almost all cases should be chosen...

    For sake of completeness: Here is how you can use a controller from another module

    (PS: Don't do this. lol :P)

    var app = angular.module('myApp', ['myDirectives']);
    
    app.controller('AppCtrl1', function($scope) {
        $scope.foo = 'bar';
    });
    
    
    var directives = angular.module('myDirectives', []);
    
    directives.directive('test', function($controller) {
       return {
           template: '

    {{foo}}

    ', link: function(scope, elem, attrs) { var controller = $controller('AppCtrl1', { $scope: scope }); console.log($scope.foo); //bar } }; });

提交回复
热议问题