7

AngularJS を使用して、「onload」引数を使用して、「子」コントローラー (含まれているテンプレートによって呼び出されるコントローラー) 内で定義された関数をトリガーすることは可能ですか?

例:

<!-- parent container -->
<div ng-include="'/path/template.html'" onload="childOnLoad()"></div>

<!-- template.html -->
<div ng-controller="childController">   
    <p ng-bind="txt"></p>
</div>

<!-- childController.js -->
app.controller('childController', function($scope) {    
    $scope.txt = "Test text";

    $scope.childOnLoad = function() {
        alert("Loaded!");
    };  
});

それは理にかなっていますか?または、次のように、単に childController 内で関数を呼び出す必要がありますか?

<!-- childController.js -->
app.controller('childController', function($scope) {    
    $scope.txt = "Test text";

    $scope.childOnLoad = function() {
        alert("Loaded!");
    };

    $scope.childOnLoad();   
});
4

2 に答える 2

2

2 回目の試行では、正しく行いました。いくつかの関数を作成しています。そのコントローラーのスコープは、それらの関数をプロパティとして取得し、次に $scope.childOnLoad(); を取得します。作成した関数を呼び出します。

実際、非常に多くの関数を定義できますが、それらはロード中またはロード完了時に呼び出されません。

于 2014-12-08T18:32:38.973 に答える