简体   繁体   English

AngularJS默认选择值

[英]AngularJS default select value

I'm building a application for my family that allows you make picks for each nfl game for each week. 我正在为我的家人构建一个应用程序,该应用程序允许您每周为每个nfl游戏做出选择。 I have a drop down that is populated with the weeks. 我有一个下拉列表,其中包含星期。 When a user selects a week, it populates the page with all the games for that week and a dropdown for each game so that the user can make their pick. 当用户选择一周时,它会在页面中填充该周的所有游戏以及每个游戏的下拉菜单,以便用户进行选择。 While the pick can be updated just fine and the binding is working, I have a problem when the page is loaded, it will not populate the dropdown with the pick that is in the database. 虽然可以很好地更新选择并且绑定正在工作,但是在加载页面时出现问题,它不会使用数据库中的选择填充下拉列表。 I've tried different things such as ng-init but no luck. 我尝试了ng-init等其他方法,但是没有运气。

I am using a Custom MEAN Stack with Jade Templates. 我正在使用带有Jade模板的自定义MEAN堆栈。 Here is a snippet of my jade 这是我的玉片

div(ng-controller='picksController')
h1 These are the picks
select.form-control(ng-model="selectedWeek", ng-change="getWeek(selectedWeek)", ng-options="week.week for week in weeks")
    option(value="") Select Week
div(ng-show="picks != null", ng-repeat="game in picks")
    hr.divider
    .panel.panel-primary
        .panel-heading
            h5 {{teamsMap[game.homeTeam].city + " " + teamsMap[game.homeTeam].name}} vs {{teamsMap[game.awayTeam].city + " " + teamsMap[game.awayTeam].name}}
            select.form-control(ng-model="game.pick")
                option(value='{{teamsMap[game.homeTeam].id}}') {{teamsMap[game.homeTeam].name}}
                option(value='{{teamsMap[game.awayTeam].id}}') {{teamsMap[game.awayTeam].name}}
        .panel-body
button.btn.btn-primary.submitButton(ng-show="picks != null", ng-click="updatePicks()") Submit

HTML version: HTML版本:

<div ng-controller='picksController'>
<h1>These are the picks</h1>
<select class="form-control" ng-model="selectedWeek", ng-change="getWeek(selectedWeek)", ng-options="week.week for week in weeks">
    <option value="">Select Week</option>
</select>
<div ng-show="picks != null", ng-repeat="game in picks">
    <hr class="divider">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h5>{{teamsMap[game.homeTeam].city + " " + teamsMap[game.homeTeam].name}} vs {{teamsMap[game.awayTeam].city + " " + teamsMap[game.awayTeam].name}}</h5>
            <select class="form-control" ng-model="game.pick">
                <option value="{{teamsMap[game.homeTeam].id}}">{{teamsMap[game.homeTeam].name}}</option>
                <option value="{{teamsMap[game.awayTeam].id}}">{{teamsMap[game.awayTeam].name}}</option>
            </select>
        </div>
        <div class="panel-body">

        </div>
    </div>
</div>
<button class="btn btn-primary submitButton" ng-show="picks != null", ng-click="updatePicks()"> Submit </button>

and my angular controller 和我的角度控制器

angular.module('app').controller('picksController', function($scope, bfNotifier, bfIdentity, bfPicks, bfWeeks, bfGames, bfTeams) {
$scope.teamsMap = {};
var teams = bfTeams.query(function() {
    for(var i = 0; i < teams.length; i++) {
        $scope.teamsMap[teams[i].id] = teams[i];
    }
});

$scope.weeks = bfWeeks.query();

$scope.getWeek = function(week) {
    if(week != null) {
        $scope.picks = bfPicks.query({"week": week.week, "user": bfIdentity.currentUser.username});
    }
}

}) })

The picks JSON is structured like so... 精选JSON的结构如下:

pick {
    user: xxx,
    week: xxx,
    game: xxx,
    homeTeam: xxx,
    awayTeam: xxx,
    pick: xxx
}

week

week {
    id: xxx,
    week: xxx
}

Even if you can't answer my question, if you can lead me in the right direction. 即使您无法回答我的问题,也可以引导我朝正确的方向前进。 Sometimes it helps to have someone else look at my code. 有时候让别人看看我的代码会有所帮助。 Thanks! 谢谢!

Your default value should be "xxx" 您的默认值应为“ xxx”
as you are linking your model "game.pick" with pick["pick"] . 当您将模型“ game.pick”与pick [“ pick”]链接时。

I could be more help if you weren't using Jade (put up a fiddle with html?), but I don't see you using ng-options anywhere. 如果您不使用Jade(用html摆弄小提琴?),我可能会得到更多帮助,但是我看不到您在任何地方使用ng-options You want to use ng-options when dealing with selects. 您要在处理ng-options时使用ng-options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM