javascript - ionic 框架 : $scope is undefined in simple alert

标签 javascript angularjs ionic-framework angularjs-scope

.controller('newGoalCtrl', function($scope, $ionicPopup) {
    $scope.addNewGoal = function() {
        alert($scope.goaltitle);
    };
});

<ion-pane view-title="goal">
   <ion-header-bar class="bar-positive">
      <div class="buttons">
          <a nav-transition="android" class="button button-icon icon ion-arrow-left-b" ng-click="" href="#/index"></a>
      </div>
      <h1 class="title">Add New Goal</h1>
    </ion-header-bar>


    <ion-content class="padding" scroll="false" >
        <div class="list">
            <label class="item item-input">
                <input type="text" placeholder="#Title" ng-model="goaltitle">
            </label>
            <label class="item item-input">
                <span class="hashtag-title">#{{hashtagname}}</span>
            </label>
            <label class="item item-input">
              <textarea placeholder="Goal"></textarea>
            </label>
        </div>
    </ion-content>


    <ion-tabs class="tabs-icon-top tabs-color-active-positive">
        <button class="button button-positive button-bar no-round-corner" ng-click="addNewGoal()">Add Goal</button>
    </ion-tabs>
</ion-pane>

这是我的代码...我不知道如何解释,但当我在文本框中输入内容时它总是说未定义...

但是 $scope.goaltitle = "something"正在 .controller() 上工作; ...

最佳答案

简答

The root cause of this issue is, ion-content does create a prototypically inherited child scope, that's why goaltitle(primitive type) of controller scope is different than the goaltitle you are using on ng-model

理想的做法是在定义 View 模型时遵循点规则。因此,范围层次结构将遵循原型(prototype)继承规则。

您应该定义对象,然后分配其中的所有 ng-model 属性。

Controller

.controller('newGoalCtrl', function($scope, $ionicPopup) {
    $scope.model = {};
    $scope.addNewGoal = function() {
        alert($scope.model.goaltitle);
    };
});

然后在其中添加goalTitleGoal等属性。

标记

<ion-content class="padding" scroll="false" >
    <div class="list">
        <label class="item item-input">
            <input type="text" placeholder="#Title" ng-model="model.goaltitle">
        </label>
        <label class="item item-input">
            <span class="hashtag-title">#{{hashtagname}}</span>
        </label>
        <label class="item item-input">
          <textarea placeholder="Goal" ng-model="model.Goal"></textarea>
        </label>
    </div>
</ion-content>

我不想再次重写整个解释,所以这里我引用similar answer ,我在其中介绍了所有详细信息。

关于javascript - ionic 框架 : $scope is undefined in simple alert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39016137/

相关文章:

javascript - 如何在 React 中提交表单后删除对输入的关注

javascript - 自定义指令上的 Angular Controller

javascript - 将图像的点击数据保存在表格js中

键盘消失时的Android白色背景

javascript - Angular 单元测试 : Browser is not defined?

angular - 在 Ionic 中更改 HTML 时避免重新加载整页

html - ionic2自定义图标大小和按钮大小

javascript - 单击其他元素/主体失败时 react 切换并关闭

android - ionic 和 cordova - css active 在 Android 设备上不起作用

php - 根据使用 ng-option 从数据库(mysql)检索的数据在下拉列表中选择特定选项