gpt4 book ai didi

javascript - 将变量传递给函数 - Angular

转载 作者:行者123 更新时间:2023-11-28 15:24:29 24 4
gpt4 key购买 nike

我想打开一个模态,声明模态文件名,以及我想在模态范围内使用的图像 URL。例如:

ng-click="showModal('screenshot','http://placehold.it/800x550')"

当我尝试上述操作时,出现以下错误:

Error: [$injector:unpr] Unknown provider: imageUrlProvider <- imageUrl <- ModalController

这是我正在尝试的代码 - 模态 Controller :

hmwsApp.controller('ModalController', function($scope, close, imageUrl) {

$scope.imageUrl = imageUrl;

$scope.close = function(result) {
close(result, 500); // close after 500ms to allow for bootstrap to animate
};

});

主 Controller :

hmwsApp.controller('mainController', ['$scope', '$http', '$rootScope', 'ModalService', function($scope, $http, $rootScope, ModalService) {

$scope.showModal = function(modalUrl, imageUrl) {
ModalService.showModal({
templateUrl: '/views/modals/' + modalUrl + '.html',
controller: "ModalController",
resolve: {
imageUrl: function () {
return imageUrl;
}
}
})
.then(function(modal) {
modal.element.modal();
modal.close.then(function(result) {
$scope.message = "Image shown " + result;
});
});
};

}]);

有什么明显错误吗?

最佳答案

你在这里做了什么:

hmwsApp.controller('ModalController', function($scope, close, imageUrl)

imageUrl注入(inject)到您的 Controller 中。

您想要做的是在 Controller 方法内创建一个名为 imageUrl 的输入参数:

hmwsApp.controller('ModalController', function($scope) {
$scope.showModal = function(screenshot,imageUrl){
// do something
}
......
});

关于javascript - 将变量传递给函数 - Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com