简体   繁体   English

ng-model 绑定在控制器中返回未定义

[英]ng-model binding returning undefined in controller

I am trying to set a controller object property to an input text that the user enters using ng-model.我正在尝试将控制器对象属性设置为用户使用 ng-model 输入的输入文本。 Although when i log the value using console it returns as undefined, can anyone point out what i might be doing wrong?尽管当我使用控制台记录该值时它返回未定义,但有人能指出我可能做错了什么吗?

Here is my HTML code这是我的 HTML 代码

<ion-view view-title="Create Goal">
    <ion-content>
        <form style="margin-top:25px">

<!--Add the input text for the goal's name-->
            <div class="content double-padding" style="padding-bottom:0px">
                <h4 class="positive">{{ "_goal_name" | translate }}</h4>
                <input type="text" ng-model="name" style="border-bottom:1px solid grey;font-size:20px;margin-left:0px;width:100%">
            </div>



<!--Add the input range for the goal's distance-->
            <div class="content double-padding" style="padding-bottom:0px">
                <h4 class="positive">{{ "_goal_distance" | translate }}</h4>
                    <input type="text" ng-model="distance" style="border-bottom:1px solid grey;font-size:20px;margin-left:0px;width:100%">
            </div>



<!--Add the input text for the goal's start date in the form of aaaa-mm-dd-->
            <div class="content double-padding" style="padding-bottom:0px">
                <h4 class="positive">{{ "_goal_startDate" | translate }}</h4>
                <input type="text" ng-model="startDate" style="border-bottom:1px solid grey;font-size:20px;margin-left:0px;width:100%">
            </div>



<!--Add the input text for the goal's finish date in the form of aaaa-mm-dd-->
<!--Only appears  if the recurring option is true-->
            <div ng-show="goals.recurrent" class="content double-padding" style="padding-bottom:0px">
                <h4 class="positive">{{ "_goal_finishDate" | translate }}</h4>
                <input type="text" name="finishDate" style="border-bottom:1px solid grey;font-size:20px;margin-left:0px;width:100%">
            </div>



<!--Add the input checkbox for the goal's recurrence option -->
            <!--<ion-toggle ng-model="goals.recurrent" ng-checked="goalRecurrent" toggle-class="toggle-positive">-->
                <!--<h4 class="positive" style="font-size:18px;">{{ "_goal_recurrence" | translate }}</h4>-->
            <!--</ion-toggle>-->



<!--Add the button that calls the function to save the new goal-->
            <div class="content double-padding" style="padding-bottom:0px">
                <button ng-click="createGoal()" class="button  button-positive button-block">{{ "_button_create" | translate }}</button>
            </div>

        </form>
    </ion-content>
</ion-view>

And here is my Controller code这是我的控制器代码

.controller("GoalFormCtrl", function($scope, $state, $stateParams) {

    $scope.createGoal = function() {
        $scope.goal = {
            name : $scope.name,
            distance : $scope.distance,
            startDate : $scope.startDate,
            // recurrent : $scope.goalRecurrent
        };

        console.log($scope.goal);
    };
  })

please check the below link, I have added this in a plunker请查看以下链接,我已将其添加到 plunker 中

ng-controller="GoalFormCtrl"

model value is not getting the controller context模型值未获取控制器上下文

http://plnkr.co/edit/81Vi92bQjjhQxsIw1axD http://plnkr.co/edit/81Vi92bQjjhQxsIw1axD

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

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