javascript - ionic 模态 : clear all fields on click/cancel

标签 javascript angularjs ionic-framework

我尝试将其与当前 View 范围一起使用,并添加自己的 Controller 。单击取消按钮时无法成功清除所有字段。我所需要的只是清理字段。

这是我的 Controller 模式:

var myApp = angular.module('starter.controllers');

function TalkSearchPageCtrl($scope, TalkSearch, $ionicModal) {
var nextPageNum = 1;
$scope.noMoreItemsAvailable = false;
var nextPageNum = 1;
$scope.loadMore = function() {
    TalkSearch.getList(nextPageNum, {

    }, $scope.idsToExclude, $scope.backResult).success(function(items) {
        if (typeof(items.idsToExclude) !== undefined) {
            $scope.idsToExclude = items.idsToExclude;
            $scope.backResult = true;
        } else {
            $scope.idsToExclude = undefined;
            $scope.backResult = undefined;
        }
        // error check when it's not loading
        if (items.talks.length != 0) {
            i = 0;
            while (i != items.talks.length - 1) {
                $scope.talks.push(items.talks[i]);
                i++;
            }
            nextPageNum++;
        } else {
            $scope.noMoreItemsAvailable = true;
        }

        $scope.$broadcast('scroll.infiniteScrollComplete');
    });


};

$scope.talks = [];

$scope.categories = [
];

$scope.checkedCategories = [];


$scope.toggleCheck = function(category) {
    if ($scope.checkedCategories.indexOf(category) === -1) {
        $scope.checkedCategories.push(category);
    } else {
          $scope.checkedCategories.splice($scope.checkedCategories.indexOf(category), 1);
    }
};
$scope.getValues = function(filter) {
    console.log(filter);
    if (filter.length || filter !== undefined) {
        $scope.userFilter = {};
        $scope.userFilter.categories = [];
        $scope.userFilter.filters = {};
        $scope.userFilter.filters = angular.copy(filter);
        var categories = $scope.checkedCategories;
        $scope.userFilter.categories = categories;
        getFiltersService.filter = angular.copy(filter);
        console.log($scope.userFilter);
        // console.log(getFiltersService.filter);
    }
};

$scope.reset = function() {
    console.log($scope.userFilter);
    // $scope.modal.hide();
    // $scope.userFilter.filters = null;
    // $scope.userFilter.categories = null;
    // $scope.userFilter = {};
    console.log($scope.userFilter);
};

$ionicModal.fromTemplateUrl('my-modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
}).then(function(modal) {
    $scope.modal = modal;
});

} myApp.controller('TalkSearchPageCtrl', ['$scope', 'TalkSearch', '$ionicModal', TalkSearchPageCtrl]);

$ionicModal.fromTemplateUrl('my-modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
}).then(function(modal) {
    $scope.modal = modal;
});

这是我的 html 脚本模板:

<script id="my-modal.html" type="text/ng-template">
<ion-modal-view>
    <ion-header-bar class="bar bar-header schoolinkscolor headerSection" ng-controller="TalkFilterPageCtrl">
        <div class="row padding0">
            <div class="col">
                <div class="button button-white closeModal" side="right" ng-click="reset(talkfilter);hideButton=false;showDetails=false;"><span>Cancel</span></div>
            </div>
            <div class="col">
                <div class="light headerTitle"><span class="light">Schoo</span><span class="color-black">Links</span></div>
            </div>
            <div class="col">
                <div class="button button-white searchButton" side="left"><span>Search</span></div>
            </div>
        </div>
    </ion-header-bar>
    <ion-content class="has-header">
        <div class="talkFilterContainer">
            <section>
                <div class="col padding0">
                    <div class="list list-inset">
                        <label class="item item-input">
                            <i class="icon ion-search placeholder-icon"></i>
                            <input type="text" placeholder="Search" ng-model="talkfilter.searchTalk" ng-change="getValues(talkfilter)" ng-model-options="{ debounce: 1000 }">
                        </label>
                    </div>
                </div>
            </section>
            <div class="row padding clearboth"></div>
            <section>
                <div class="list padding">
                    <label class="item item-input item-select">
                        <div class="input-label">
                            Language:
                        </div>
                        <select ng-options="author for author in ['Mandarin','Spanish','Portuguese']" ng-model="talkfilter.selectLanguage" ng-init="author = 'Select Preferred Language'" ng-change="getValues(talkfilter)">
                            <option value="">Select Preferred Language</option>
                        </select>
                    </label>
                </div>
            </section>
            <div class="row padding clearboth"></div>
            <section>
                <div class="list padding">
                    <label class="item item-input item-select">
                        <div class="input-label">
                            Author Type:
                        </div>
                        <select ng-options="author for author in ['Consultant','School','Student']" ng-model="talkfilter.authorType" ng-init="author = 'Select By Author Type'" ng-change="getValues(talkfilter)">
                            <option value="">Select By Author Type</option>
                        </select>
                    </label>
                </div>
            </section>
            <div class="row padding clearboth"></div>
            <section class="padding">
                <ion-list>
                    <ion-item ng-repeat="category in categories track by $index" ng-class="{ 'hidden': ! showDetails  && $index > 2}">
                        <ion-checkbox value="{{category}}" ng-model="$index" ng-click="toggleCheck(category); getValues(talkfilter)">{{category}}</ion-checkbox>
                    </ion-item>
                </ion-list>
                <div class="row">
                    <button class="button button-full button-clear" ng-click="showDetails = ! showDetails;hideButton=true" ng-show="!hideButton">See All Categories</button>
                </div>
            </section>
        </div>
    </ion-content>
</ion-modal-view>

这是我的重置功能:

var Talk = this;
Talk.reset = function(filter) {
    console.log(filter);
    filter = null;
};

还尝试过 $scope :

$scope.reset = function(filter) {
    console.log(filter);
    filter = null;
};

***更新:

$scope.reset = function(filter) {
     $scope.userFilter = null; //does not work. none of these variations work.
     $scope.userFilter = '';
};

这两个方法都返回未定义。但如果我将按钮放在正文中,它会清除字段。我需要标题中的按钮。

还尝试使用以下方式内联重置:

<button ng-click="talkfilter=null"></button>

最佳答案

问题是您没有通过 ng-click 传递过滤器函数中的任何内容,因为您的 $scope.userfilter 未在整个 Controller 中定义。

像您在 getValues 内部所做的那样添加 $scope.userFilter = {} ,如果您像这样执行重置功能,那么在该函数外部应该会得到所需的结果......

function TalkSearchPageCtrl($scope, TalkSearch, $ionicModal) {
$scope.userFilter = {};

...

$scope.reset = function(filter) {
    console.log(filter); \\Make sure this is the object
    $scope.userFilter = null; \\Set it to null or {}
}`

关于javascript - ionic 模态 : clear all fields on click/cancel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462671/

相关文章:

javascript - Angular 指令移除元素绑定(bind)

javascript - angularJs - 如何获取过滤数组

html - 按钮上的背景图像不显示

javascript - 如何确保 nodejs 的顺序执行以从查询返回结果?

javascript - 从服务器加载已经存在的 HTML 文件而不是浏览器缓存

javascript - 允许网络摄像头在本地应用程序中不发出警告

ios - ionic3 ios13 InApp浏览器崩溃

javascript - 如何从 Angular 和 Typescript 中的选项卡数组中禁用选项卡元素

javascript - 音频结束后隐藏按钮

android - Chrome Web View : triggering native Android Share function