admin管理员组文章数量:1023827
I have created a directive inside my controller, which i want to include another controller to the directive. The error i get back is 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('ponentsByModule', module_id))
.success(function (response) {
$scopeponents = response;
});
}]);
app.directive('viewDoc', function () {
return {
templateUrl: "tpl/directives/testview/document.html",
controller: "js/controllers/testview/DocumentController.js",
resolve: { ponents: function() { return $scopeponents }}
};
});
DocumentController
app.controller('DocumentController', ['$http', '$scope', '$sessionStorage', '$state', '$log', 'Session', 'api', 'ponents', function ($http, $scope, $sessionStorage, $state, $log, Session, api, ponents) {
$scopeponents = ponents;
}]);
I'm pretty new with directices, but does anyone have any idea what I'm doing wrong?
I have created a directive inside my controller, which i want to include another controller to the directive. The error i get back is 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('ponentsByModule', module_id))
.success(function (response) {
$scope.ponents = response;
});
}]);
app.directive('viewDoc', function () {
return {
templateUrl: "tpl/directives/testview/document.html",
controller: "js/controllers/testview/DocumentController.js",
resolve: { ponents: function() { return $scope.ponents }}
};
});
DocumentController
app.controller('DocumentController', ['$http', '$scope', '$sessionStorage', '$state', '$log', 'Session', 'api', 'ponents', function ($http, $scope, $sessionStorage, $state, $log, Session, api, ponents) {
$scope.ponents = ponents;
}]);
I'm pretty new with directices, but does anyone have any idea what I'm doing wrong?
Share Improve this question asked Mar 30, 2015 at 14:23 BackerBacker 1,1141 gold badge21 silver badges33 bronze badges 1-
In that case controller is not a js file, it's actually controller name in scope of app i.e.
controller: "TestviewController"
– maurycy Commented Mar 30, 2015 at 14:43
4 Answers
Reset to default 3Inside the directive definition object, the controller
property expects a string with the function name, or the function itself (not the path to script file).
app.directive('viewDoc', function () {
return {
...
controller: "DocumentController",
};
});
You want to call the controller by name, not by file name:
controller: "js/controllers/testview/DocumentController.js"
should be
controller: "DocumentController"
There is no option to put controller by its URL in the directive definition. However if you define your controller in DOM template you could use controller: 'myController as myCtrl'
in directive definition
are you sure you need a directive controller? i think what you are trying to achieve is link function.
you can use directive link functions like controllers.
.directive('myDialog', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
templateUrl: 'my-dialog.html',
link: function (scope, element) {
scope.name = 'Jeff';
}
};
});
take a look at angular docs https://docs.angularjs/guide/directive
I have created a directive inside my controller, which i want to include another controller to the directive. The error i get back is 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('ponentsByModule', module_id))
.success(function (response) {
$scopeponents = response;
});
}]);
app.directive('viewDoc', function () {
return {
templateUrl: "tpl/directives/testview/document.html",
controller: "js/controllers/testview/DocumentController.js",
resolve: { ponents: function() { return $scopeponents }}
};
});
DocumentController
app.controller('DocumentController', ['$http', '$scope', '$sessionStorage', '$state', '$log', 'Session', 'api', 'ponents', function ($http, $scope, $sessionStorage, $state, $log, Session, api, ponents) {
$scopeponents = ponents;
}]);
I'm pretty new with directices, but does anyone have any idea what I'm doing wrong?
I have created a directive inside my controller, which i want to include another controller to the directive. The error i get back is 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('ponentsByModule', module_id))
.success(function (response) {
$scope.ponents = response;
});
}]);
app.directive('viewDoc', function () {
return {
templateUrl: "tpl/directives/testview/document.html",
controller: "js/controllers/testview/DocumentController.js",
resolve: { ponents: function() { return $scope.ponents }}
};
});
DocumentController
app.controller('DocumentController', ['$http', '$scope', '$sessionStorage', '$state', '$log', 'Session', 'api', 'ponents', function ($http, $scope, $sessionStorage, $state, $log, Session, api, ponents) {
$scope.ponents = ponents;
}]);
I'm pretty new with directices, but does anyone have any idea what I'm doing wrong?
Share Improve this question asked Mar 30, 2015 at 14:23 BackerBacker 1,1141 gold badge21 silver badges33 bronze badges 1-
In that case controller is not a js file, it's actually controller name in scope of app i.e.
controller: "TestviewController"
– maurycy Commented Mar 30, 2015 at 14:43
4 Answers
Reset to default 3Inside the directive definition object, the controller
property expects a string with the function name, or the function itself (not the path to script file).
app.directive('viewDoc', function () {
return {
...
controller: "DocumentController",
};
});
You want to call the controller by name, not by file name:
controller: "js/controllers/testview/DocumentController.js"
should be
controller: "DocumentController"
There is no option to put controller by its URL in the directive definition. However if you define your controller in DOM template you could use controller: 'myController as myCtrl'
in directive definition
are you sure you need a directive controller? i think what you are trying to achieve is link function.
you can use directive link functions like controllers.
.directive('myDialog', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
templateUrl: 'my-dialog.html',
link: function (scope, element) {
scope.name = 'Jeff';
}
};
});
take a look at angular docs https://docs.angularjs/guide/directive
本文标签: javascriptAngular directive controller Argument is not a functiongot undefinedStack Overflow
版权声明:本文标题:javascript - Angular directive controller: Argument is not a function, got undefined - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745516386a2154077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论