-1

最初の環境: angularjs 1.2 rc1

親と子の 2 つのディレクティブが必要なので、require: trueを使用しますが、問題が発生します。

Error: [$compile:ctreq] Controller 'testParent', required by directive 'testChildren', can't be found!
http://errors.angularjs.org/1.2.0-rc.2/$compile/ctreq?p0=testParent&p1=testChildren
    at http://127.0.0.1/static/lib/angular/1.2.0rc2/angular.js:78:12
    at getControllers (http://127.0.0.1/static/lib/angular/1.2.0rc2/angular.js:4981:19)
    at nodeLinkFn (http://127.0.0.1/static/lib/angular/1.2.0rc2/angular.js:5122:35)
    at compositeLinkFn (http://127.0.0.1/static/lib/angular/1.2.0rc2/angular.js:4640:15)
    at nodeLinkFn (http://127.0.0.1/static/lib/angular/1.2.0rc2/angular.js:5115:24)
    at compositeLinkFn (http://127.0.0.1/static/lib/angular/1.2.0rc2/angular.js:4640:15)
    at publicLinkFn (http://127.0.0.1/static/lib/angular/1.2.0rc2/angular.js:4549:30)
    at http://127.0.0.1/static/lib/angular/1.2.0rc2/angular-route.js:854:15
    at publicLinkFn (http://127.0.0.1/static/lib/angular/1.2.0rc2/angular.js:4548:29)
    at update (http://127.0.0.1/static/lib/angular/1.2.0rc2/angular-route.js:832:13) <div test-children=""> 

私のコードは単純です(4つのテストのみ):

angular.module('app', [])


.directive('testParent', [function() {
    return {
        restrict: 'EA',

        link: function link(scope, element, attr) {
            element.addClass('abcdef')
        }
    }
}])

.directive('testChildren', [function() {
    return {
        restrict: 'EA',
        require: '^testParent',

        link: function(scope, element, attr) {
            // console.log(ctrl)
                // ctrl.setTest('It is test')
                element.append('inner text')
        }
    }
}])

何が起こるか誰が教えてくれますか???

4

1 に答える 1

1

親ディレクティブはコントローラーを提供する必要があります。これは、子のリンク関数のオプションの 4 番目のパラメーターとして渡されます。

angular.module('app', [])
.directive('testParent', [function() {
    return {
        restrict: 'EA',
        controller: function() { 
            //... must include a controller in parent 
            this.doSomething = () => {}
        }
    }
}])

.directive('testChildren', [function() {
    return {
        restrict: 'EA',
        require: '^testParent',

        link: function(scope, element, attr, parent) {
            parent.doSomething()
        }
    }
}])
于 2013-12-09T17:33:39.217 に答える