【问题标题】:Getting the current month by default in a dropdownlist默认情况下在下拉列表中获取当前月份
【发布时间】:2015-08-03 05:16:29
【问题描述】:

我在一个项目中工作,第一次访问的用户默认可以看到当月的内容。 但是如果他们想查看另一个月份的内容,他们需要从下拉列表中选择月份和年份。

<div class="row" ng-show="firstVisit">
        <div class="col">
            <select ng-init="item1=m+1" ng-model="item1" ng-change="onchangeMonth()" style="width:100%;text-align:center;">
                    <option style="text-indent:30%" ng-repeat="item1 in IdeaMonthList" value="{{item1}}" >{{item1}}</option>
            </select>
        </div>
    </div>
    <div class="row" ng-hide="firstVisit">
        <div class="col">
            <select ng-model="item1" ng-change="onchangeMonth()" style="width:100%;text-align:center;">
                    <option style="text-indent:30%" ng-repeat="item1 in IdeaMonthList" value="{{item1}}" >{{item1}}</option>
            </select>
        </div>
    </div>

如果用户是第一次访问,它应该显示当前月份的详细信息,我可以获取内容,但选择的月份不在下拉列表中。

$scope.firstVisit=true;
var d = new Date();
var m = d.getMonth();
var IdeaMonth=["1","2","3","4","5","6","7","8","9","10","11","12"]
$scope.IdeaMonthList=IdeaMonth;
$rootScope.MonthSelected=m+1;
$scope.onchangeMonth = function() {
        selectedValue=$scope.item1;
        $rootScope.MonthSelected = selectedValue;
     };

【问题讨论】:

    标签: angularjs ionic


    【解决方案1】:

    如果 firstVisit 是true,您将显示两个 ng-shows。我想其中一个应该是ng-show=!firstVisit

    难道你没有一个笨拙的人来证明问题所在吗?下拉菜单中没有选择默认月份,是这个问题吗?

    【讨论】:

    • 不要看这两个firstVist。我只想在用户打开该页面时在下拉列表中选择当前月份。
    • 您的示例代码非常混乱,但您肯定做错了什么。你不应该使用 ng-init 来初始化你的 item1。您的 ng-repeat 应该看起来像 ng-repeat="item in IdeaMonthList" 并且在您的 angularapp 中您应该使用当前月份的值初始化 item1。
    【解决方案2】:

    你应该是$scope.item1 = m+1 还是$scope.item1 = $rootScope.MonthSelected

    但是,您不需要使用$rootScope 来存储月份。

    【讨论】:

    • 我没听懂。当 firstVisit =true 时,下拉列表中的值应为 m+1,但每次都为 1。
    • ng-model 的下拉菜单是 item1,所以如果你将模型的值设置为 m+1,它应该可以工作。请尝试一下。
    • 我试过了,它不起作用。您可以通过此链接stackoverflow.com/questions/25722578/… 获得对我的问题的参考。在该链接中,选项是静态的,因此人们可以使用选择的选项,但在我的选项中,使用 ng-repeat 是动态的。
    猜你喜欢
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2016-09-19
    • 2017-09-08
    • 2018-03-07
    • 1970-01-01
    相关资源
    最近更新 更多