使用AngularJS将选择重置为默认值 [英] Reset select to default value with AngularJS

查看:134
本文介绍了使用AngularJS将选择重置为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对AngularJS有以下问题.我有一个 选择使用ngOptions创建的选项.我想要 将选择的选项设置回默认选项.我试过了 删除模型变量,例如:

if(angular.isDefined($scope.first)){
    delete $scope.first;
}

但是这不起作用.这是我的HTML.

    <div ng-app="app">
        <div ng-controller="testCtrl">
            <select data-ng-model="first"  data-ng-options="item.name for item in selectContent" required="required">
              <option value="" style="display: none;">-- select --</option>
            </select>
            {{first.value}}
<hr/>
        <input type="button" value="reset dropdown" data-ng-click="resetDropDown()"/>
        </div>
    </div>

这是我的JavaScript代码:

angular.module('app', []).controller('testCtrl', function ($scope) {
    $scope.selectContent = [
        {
            name: "first",
            value: "1"
        },
        {
            name: "second",
            value: "2"
        }
    ];

    $scope.resetDropDown = function() {
        if(angular.isDefined($scope.first)){
            delete $scope.first;
        }
    }
});

正在使用jsfiddle:

http://jsfiddle.net/zono/rzJ2w/

我该如何解决这个问题?

最好的问候.

解决方案

您的重置按钮位于包含控制器的div之外.这意味着您尝试使用它的上下文中不存在您的重置功能.<​​/p>

更改为:

<div ng-app="app">
    <div ng-controller="testCtrl">
        <select data-ng-model="first"  data-ng-options="item.name for item in selectContent" required="required">
          <option value="" style="display: none;">-- select --</option>
        </select>
        {{first.value}}
        <hr/>
        <input type="button" value="reset dropdown" data-ng-click="resetDropDown()"/>
    </div>
</div>

使用调试器来确保要尝试修复的代码确实在执行中总是一个好主意.

I have a following question for AngularJS. I have a select with options created with ngOptions. I want to set selected option back to default option. I tried to delete model variable e.g:

if(angular.isDefined($scope.first)){
    delete $scope.first;
}

But this not working. Here is my html.

    <div ng-app="app">
        <div ng-controller="testCtrl">
            <select data-ng-model="first"  data-ng-options="item.name for item in selectContent" required="required">
              <option value="" style="display: none;">-- select --</option>
            </select>
            {{first.value}}
<hr/>
        <input type="button" value="reset dropdown" data-ng-click="resetDropDown()"/>
        </div>
    </div>

And here is my JavaScript code:

angular.module('app', []).controller('testCtrl', function ($scope) {
    $scope.selectContent = [
        {
            name: "first",
            value: "1"
        },
        {
            name: "second",
            value: "2"
        }
    ];

    $scope.resetDropDown = function() {
        if(angular.isDefined($scope.first)){
            delete $scope.first;
        }
    }
});

Here is working jsfiddle:

http://jsfiddle.net/zono/rzJ2w/

How I can solve this problem?

Best regards.

解决方案

Your reset-button is placed outside of the div containing your controller. This means that your reset-function does not exist in the context where you try to use it.

Change to:

<div ng-app="app">
    <div ng-controller="testCtrl">
        <select data-ng-model="first"  data-ng-options="item.name for item in selectContent" required="required">
          <option value="" style="display: none;">-- select --</option>
        </select>
        {{first.value}}
        <hr/>
        <input type="button" value="reset dropdown" data-ng-click="resetDropDown()"/>
    </div>
</div>

It's always a good idea to use a debugger to make sure the code you're trying to fix is actually being executed.

这篇关于使用AngularJS将选择重置为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆