javascript - angularjs - ng-model 没有为我设置默认值

标签 javascript angularjs

ng-model 在这里设置,所以我可以将过滤器传递到我的函数中并获得所有用户选择。我想我也可以用它来预填充/默认数据。它不工作。这是我的 Controller :

.controller('ListCtrl', [
    '$scope', '$filter', '$location', 'context', 'breeze', 'Service',
    function ($scope, $filter, $location, context, breeze, Service) {
        $scope.pageLoaded = true;
        $scope.lists = [];
        $scope.DDL1 = '';
        $scope.filter = {
            fromDate: '2015-05-01',
            toDate: '',
            sales: false            
        };

我有这样的 html:

            <md-content ng-controller="ListCtrl" layout="column" flex class="md-padding">
                <md-tabs class="md-primary clearfix" md-selected="0" flex>              
                    <md-tab label="Lists">
                        <table><tr>
                                <td width="10%">From Date: </td>
                                <td width="40%"><input type="date" ng-model="filter.fromDate" value="{{filter.fromDate}}"/></td>
<td width="10%">SParts: </td>
                                <td width="40%">                                    
                                    <input type="radio" name="sales" ng-model="filter.sales" value="true"> yes
                                    <input type="radio" name="sales" ng-model="filter.sales" value="false" checked> no
                                </td>...

最佳答案

使用字符串

由于您使用的是 input type="radio",因此将 filter.sales 的类型设置为字符串,这应该可以工作

$scope.filter = {
    fromDate: '2015-05-01',
    toDate: '',
    sales: 'false'          
};

来自 input type="radio" ng-model docs ,

The value to which the ngModel expression should be set when selected. Note that value only supports string values, i.e. the scope model needs to be a string, too. Use ngValue if you need complex models (number, object, ...).

关于javascript - angularjs - ng-model 没有为我设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35134917/

相关文章:

AngularJS - 从子指令访问父指令属性

javascript - 如何使用javascript在客户端系统中提取Zip文件

javascript - Pushwoosh 向除某些用户之外的所有人发送通知

c# - 在密码字段中显示星号而不是无值

javascript - 如何以 Angular 形式为单选按钮添加 'tabindex'

javascript - AngularJS : Scope value changing, 但并不总是应用 ng-class

javascript - HTML 文件不从 javascript 文件中提取信息

javascript - 使用 Angular 将 HTTP 数组值从服务导入到组件中

javascript - 如何使用 AngularJS 从单个选择中获取多个模型值?

angularjs - 带过滤器的AngularJS ng-repeat-NotArray错误