单击时如何打开 Ionic Modal?
[英] How to open up an Ionic Modal upon click?
本文介绍了单击时如何打开 Ionic Modal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Ionic 和 AugularJS 的新手,在单击设置页面中用于主题标签搜索的第三个选项上的复选框/单选按钮时,我在打开模态框时遇到问题.我已经包含了一个 ng-controller
和 ng-click
来采取行动.我查看了调试器,它显示了一个错误:
GET http://localhost:8100/hashtag-modal [HTTP/1.1 404 Not Found 1ms]
我知道 404 Not Found 意味着它没有找到 templateUrl 但我拥有的每个导航页面都在 index.html 中,它们工作正常,除了 hashtag-modal.html
在 >app.js
文件.为什么它不起作用,我该如何解决这个问题?
app.js
//导航页面app.config(function($stateProvider, $urlRouterProvider) {$stateProvider.state('索引', {网址:'/索引',模板网址:'index.html'}).state('关于', {网址:'/关于',templateUrl: 'about.html'}).state('设置', {网址:'/设置',templateUrl: 'settings.html'}).state('hashtag-modal', {url: '/hashtag-modal',templateUrl: 'hashtag-modal',控制器:'hashtag-modalCtrl'})$urlRouterProvider.otherwise("/index");//如果没有找到 url,则返回索引})//标签搜索选项app.controller('hashtagController', ['$scope', function($scope){$scope.hashtagValue = '搜索';//默认值$scope.hashtag = function(){$scope.hashtagValue = 'blackandwhitephotography';//如果选中,它会显示这个值};}]);//标签搜索模式app.controller('hashtag-modalCtrl', function($scope, $ionicModal) {$ionicModal.fromTemplateUrl('hashtag-modal.html', {范围:$范围,动画:向上滑动",焦点第一输入:真}).then(函数(模态){$scope.modal = 模态;});$scope.openModal = function() {$scope.modal.show();};$scope.closeModal = function() {$scope.modal.hide();};//完成后清理模态!$scope.$on('$destroy', function() {$scope.modal.remove();});//对隐藏模态执行操作$scope.$on('modal.hidden', function() {//执行动作});//执行移除模态的动作$scope.$on('modal.removed', function() {//执行动作});});
index.html
<script id="settings.html" type="text/ng-template"><!-- 离子视图的标题将显示在导航栏上--><ion-view title="设置" hide-back-button="false"><ion-content class="padding"><!-- 页面内容--><p>选中以下选项之一以设置您希望如何分类和查看 Instagram 照片.</p><div class="settings-list"><label class="item item-radio"><input type="radio" name="settings-group" value="recent"><div class="item-content">最近的
<i class="radio-icon ion-checkmark"></i>标签><label class="item item-radio"><input type="radio" name="settings-group" value="popular"><div class="item-content">最受欢迎
<i class="radio-icon ion-checkmark"></i>标签><label class="item item-radio" id="hashtagRadio" ng-controller="hashtagController" ng-click="hashtag();modal.show();"><input type="radio" name="settings-group" value="search"><div class="item-content"><span class="ion-pound"></span> <span id="hashtagInput">{{hashtagValue}}</span>
<i class="radio-icon ion-checkmark"></i>标签>
</离子含量></ion-view><!-- 标签搜索模式--><script id="hashtag-modal.html" type="text/ng-template"><ion-modal-view hide-back-button="false"><离子头栏><h1 class="title">标签搜索</h1></ion-header-bar><离子含量><label class="item item-input"><i class="icon ion-search placeholder-icon"></i><输入类型=搜索"占位符=搜索">标签></离子含量></ion-modal-view>
修改了 app.js
我在 hashtagController
中添加了一个 $ionicModal
,但据说 Error: $ionicModal is undefined
.还有什么地方是未定义的?
//标签搜索选项app.controller('hashtagController', ['$scope', function($scope, $ionicModal){$scope.hashtagValue = '搜索';//默认值$scope.hashtag = function(){$scope.hashtagValue = 'blackandwhitephotography';//如果选中,它会显示这个值$ionicModal.fromTemplateUrl('hashtag-modal.html', {范围:$范围,动画:向上滑动",焦点第一输入:真}).then(函数(模态){$scope.modal = 模态;});$scope.openModal = function() {$scope.modal.show();};$scope.closeModal = function() {$scope.modal.hide();};//完成后清理模态!$scope.$on('$destroy', function() {$scope.modal.remove();});//对隐藏模态执行操作$scope.$on('modal.hidden', function() {//执行动作});//执行移除模态的动作$scope.$on('modal.removed', function() {//执行动作});}}]);
修订标签
<input type="radio" name="settings-group" value="search"><div class="item-content"><span class="ion-pound"></span> <span id="hashtagInput">{{hashtagValue}}</span>
<i class="radio-icon ion-checkmark"></i>标签>
解决方案
ionic modal 与路由无关.
您只需从服务器加载一个静态 html 模板,该 html 就会以离子模式显示,并带有所有绑定.
与其声明一个单独的控制器,不如将它移动到同一个控制器中:
app.controller('hashtagController', ['$scope', function($scope, $ionicModal) {$scope.hashtag = function() {$scope.hashtagValue = 'blackandwhitephotography';//如果选中,它会显示这个值$ionicModal.fromTemplateUrl('hashtag-modal.html', {范围:$范围,动画:向上滑动",焦点第一输入:真}).then(函数(模态){$scope.modal = 模态;$scope.modal.show();});};$scope.openModal = function() {$scope.modal.show();};$scope.closeModal = function() {$scope.modal.hide();};$scope.$on('$destroy', function() {$scope.modal.remove();});$scope.$on('modal.hidden', function() {//执行动作});$scope.$on('modal.removed', function() {//执行动作});}
然后在您的 HTML 中:
<input type="radio" name="settings-group" value="search"><div class="item-content"><span class="ion-pound"></span> <span id="hashtagInput">{{hashtagValue}}</span>
<i class="radio-icon ion-checkmark"></i>标签>
I am new to Ionic and AugularJS and I am having a problem opening up a modal box upon click on a checkbox/radio button on the third option for hashtag search in the settings page. I have included an ng-controller
and ng-click
to take action. I looked in the debugger and it displayed an error:
GET http://localhost:8100/hashtag-modal [HTTP/1.1 404 Not Found 1ms]
I know that 404 Not Found means it didn't find the templateUrl but every nav page I have is in the index.html and they work fine except hashtag-modal.html
in the app.js
file. Why isn't it working and how do I resolve this issue?
app.js
// Navigation pages
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('index', {
url: '/index',
templateUrl: 'index.html'
})
.state('about', {
url: '/about',
templateUrl: 'about.html'
})
.state('settings', {
url: '/settings',
templateUrl: 'settings.html'
})
.state('hashtag-modal', {
url: '/hashtag-modal',
templateUrl: 'hashtag-modal',
controller: 'hashtag-modalCtrl'
})
$urlRouterProvider.otherwise("/index"); // if no url found, go back to index
})
// Hashtag Search option
app.controller('hashtagController', ['$scope', function($scope)
{
$scope.hashtagValue = 'Search'; // default value
$scope.hashtag = function()
{
$scope.hashtagValue = 'blackandwhitephotography'; // if selected, it'll display this value
};
}]);
// hashtag search modal
app.controller('hashtag-modalCtrl', function($scope, $ionicModal) {
$ionicModal.fromTemplateUrl('hashtag-modal.html', {
scope: $scope,
animation: 'slide-in-up',
focusFirstInput: true
}).then(function(modal) {
$scope.modal = modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
// Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// Execute action on hide modal
$scope.$on('modal.hidden', function() {
// Execute action
});
// Execute action on remove modal
$scope.$on('modal.removed', function() {
// Execute action
});
});
index.html
<!-- SETTINGS -->
<script id="settings.html" type="text/ng-template">
<!-- The title of the ion-view will be shown on the navbar -->
<ion-view title="Settings" hide-back-button="false">
<ion-content class="padding">
<!-- The content of the page -->
<p>Check one of the options below to set how you wish to categorize and view your Instagram photos.
</p>
<div class="settings-list">
<label class="item item-radio">
<input type="radio" name="settings-group" value="recent">
<div class="item-content">
Recent
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
<label class="item item-radio">
<input type="radio" name="settings-group" value="popular">
<div class="item-content">
Most Popular
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
<label class="item item-radio" id="hashtagRadio" ng-controller="hashtagController" ng-click="hashtag();modal.show();">
<input type="radio" name="settings-group" value="search">
<div class="item-content">
<span class="ion-pound"></span> <span id="hashtagInput">{{hashtagValue}}</span>
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
</div>
</ion-content>
</ion-view>
</script>
<!-- HASHTAG SEARCH MODAL -->
<script id="hashtag-modal.html" type="text/ng-template">
<ion-modal-view hide-back-button="false">
<ion-header-bar>
<h1 class="title">Hashtag Search</h1>
</ion-header-bar>
<ion-content>
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" placeholder="Search">
</label>
</ion-content>
</ion-modal-view>
</script>
Revised app.js
I added an $ionicModal
to the hashtagController
but it is said Error: $ionicModal is undefined
. Where else is it undefined?
// Hashtag Search option
app.controller('hashtagController', ['$scope', function($scope, $ionicModal)
{
$scope.hashtagValue = 'Search'; // default value
$scope.hashtag = function()
{
$scope.hashtagValue = 'blackandwhitephotography'; // if selected, it'll display this value
$ionicModal.fromTemplateUrl('hashtag-modal.html', {
scope: $scope,
animation: 'slide-in-up',
focusFirstInput: true
}).then(function(modal) {
$scope.modal = modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
// Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// Execute action on hide modal
$scope.$on('modal.hidden', function() {
// Execute action
});
// Execute action on remove modal
$scope.$on('modal.removed', function() {
// Execute action
});
}
}]);
Revised label
<label class="item item-radio" id="hashtagRadio" ng-controller="hashtagController" ng-click="hashtag();openModal();">
<input type="radio" name="settings-group" value="search">
<div class="item-content">
<span class="ion-pound"></span> <span id="hashtagInput">{{hashtagValue}}</span>
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
解决方案
ionic modal has nothing to do with routes.
You just load a static html template from server, and that same html is shown in ionic modal with all the bindings.
Instead of declaring a seperate controller, move it inside the same controller :
app.controller('hashtagController', ['$scope', function($scope, $ionicModal) {
$scope.hashtag = function() {
$scope.hashtagValue = 'blackandwhitephotography'; // if selected, it'll display this value
$ionicModal.fromTemplateUrl('hashtag-modal.html', {
scope: $scope,
animation: 'slide-in-up',
focusFirstInput: true
}).then(function(modal) {
$scope.modal = modal;
$scope.modal.show();
});
};
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
$scope.$on('modal.hidden', function() {
// Execute action
});
$scope.$on('modal.removed', function() {
// Execute action
});
}
Then in your HTML :
<label class="item item-radio" id="hashtagRadio" ng-controller="hashtagController" ng-click="hashtag();openModal();">
<input type="radio" name="settings-group" value="search">
<div class="item-content">
<span class="ion-pound"></span> <span id="hashtagInput">{{hashtagValue}}</span>
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
这篇关于单击时如何打开 Ionic Modal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文