[英]Angular directive controller: Argument is not a function, got undefined
我在控制器內創建了一個指令,我想在指令中包含另一個控制器。 我回來的Error: [ng:areq] Argument 'js/controllers/testview/DocumentController.js' is not a function, got undefined
是Error: [ng:areq] Argument 'js/controllers/testview/DocumentController.js' is not a function, got undefined
TestviewController
app.controller('TestviewController', ['$http', '$scope', '$sessionStorage', '$state', '$log', 'Session', 'api', function ($http, $scope, $sessionStorage, $state, $log, Session, api) {
var module_id = $state.params.id;
$http.get(api.getUrl('componentsByModule', module_id))
.success(function (response) {
$scope.components = response;
});
}]);
app.directive('viewDoc', function () {
return {
templateUrl: "tpl/directives/testview/document.html",
controller: "js/controllers/testview/DocumentController.js",
resolve: { components: function() { return $scope.components }}
};
});
DocumentController
app.controller('DocumentController', ['$http', '$scope', '$sessionStorage', '$state', '$log', 'Session', 'api', 'components', function ($http, $scope, $sessionStorage, $state, $log, Session, api, components) {
$scope.components = components;
}]);
我對指令還很陌生,但是有人知道我在做什么錯嗎?
在指令定義對象內, controller
屬性需要一個包含函數名稱或函數本身的字符串(而不是腳本文件的路徑)。
app.directive('viewDoc', function () {
return {
...
controller: "DocumentController",
};
});
您要通過名稱而不是文件名來調用控制器:
controller: "js/controllers/testview/DocumentController.js"
應該
controller: "DocumentController"
在指令定義中沒有選擇通過其URL放置控制器。 但是,如果您在DOM模板中定義控制器,則可以在指令定義中使用controller: 'myController as myCtrl'
您確定需要指令控制器嗎? 我認為您想要實現的是鏈接功能。
您可以使用控制器等指令鏈接功能。
.directive('myDialog', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
templateUrl: 'my-dialog.html',
link: function (scope, element) {
scope.name = 'Jeff';
}
};
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.