我在似乎无法正常工作的 PHP 部分页面中有一个 Angular 函数。该函数附加到从 MySQL 数据库加载到页面上的所有图像,并使用ng-click="vm.openModal('custom-modal-1')"
.
<div id="screenings">
<?php
...
//connect to MySQL database
...
while ($row = mysqli_fetch_array($result)){
echo "<div class='img_div'>";?>
//here is the ng-click that calls the Angular function
<img class="modal_img img_screenings" ng-click="vm.openModal('custom-modal-1')" src='images/<?php echo $row['image']."' >";
...
echo "</div>";
}
?>
</div>
//here is the modal with the id that gets passed to the Angular function
<modal id="custom-modal-1">
<div class="modal">
<div class="modal-body">
<img id="popup_img" src="#">
</div>
</div>
<div class="modal-background"></div>
</modal>
在文件 app.js 中,这是我试图调用的函数:
app.controller('screeningsController', ['$scope', '$log', "ModalService", function($scope, $log, ModalService){
var vm = this;
//outputs "Hello World!" to browser console as a test
vm.message = "Hello World!";
$log.log(vm.message);
vm.openModal = openModal;
vm.closeModal = closeModal;
function openModal(id){
ModalService.Open(id);
}
function closeModal(id){
ModalService.Close(id);
}
}]);
加载部分页面时,测试消息“Hello World!” 在 Angular 函数中找到的那个会触发,并且消息会出现在浏览器控制台中。但是,当在页面上单击任何类ng-click
时,不会做任何其他事情。img
modal_img
此代码的所有 Angular 模态窗口材料均取自:http: //jasonwatmore.com/post/2016/07/13/angularjs-custom-modal-example-tutorial
EDIT Controller as
在 app.js 文件的顶部声明,如下所示:
var app = angular.module('app', ['ngRoute', 'ui.router']);
app.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.hashPrefix('');
$urlRouterProvider.otherwise("/");
$stateProvider
.state('home', {
url: '/',
templateUrl: 'pages/main.html',
controller: 'mainController',
controllerAs: 'vm'
})
.state('screenings', {
url: '/screenings',
templateUrl: 'pages/screenings.php',
controller: 'screeningsController',
controllerAs: 'vm'
})
//etc