学习角度(与Ionic一起)。
我尽量避免使用$ scope并坚持使用控制器作为语法('尽可能多地使用控制器,以及HTML中的控制器)。几天前,我添加了一个在线发现的服务,以帮助更好地管理我的模态对话框。该代码附后。
angular.module('myApp').service('RefFilterModal', RefFilterModal);
function RefFilterModal($rootScope, $ionicModal) {
let templateUrl = 'client/templates/modals/refFilterModal.html';
this.showModal = showModal;
this.hideModal = hideModal;
////////////
function showModal () {
this._scope = $rootScope.$new();
$ionicModal.fromTemplateUrl(templateUrl, {
scope: this._scope,
animation: 'slide-in-up',
focusFirstInput: false,
backdropClickToClose: true
}).then((modal) => {
this._modal = modal;
modal.show();
});
}
function hideModal () {
this._scope.$destroy();
this._modal.remove();
}
}
我的困境是,通过这项服务(提出模式)$ scope和'这个'不像其他地方一样工作。例如,提交函数 - 当从ng-click调用提交函数时,年份和制造商的值未定义。如果我改变了" this.submit"到" $ scope.submit"他们是定义的。
有人可以告诉我为什么会这样 - 我希望能用到这一切。另外,我可以做些什么来实现我的目标,即坚持使用控制器作为原则?谢谢。
this.submit = function() {
RefFilters.setYearFilter(this.year);
RefFilters.setMakerFilter(this.manufacturer);
RefFilterModal.hideModal();
}
(html提交为" refFilter.submit"," refFilter.year"和" refFilter.manufacturer")。