【问题标题】:Ionic Framework: $scope is undefined in simple alertIonic 框架:$scope 在简单警报中未定义
【发布时间】:2016-07-10 17:11:28
【问题描述】:
.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(); ...

【问题讨论】:

    标签: javascript angularjs ionic-framework angularjs-scope


    【解决方案1】:

    简答

    这个问题的根本原因是,ion-content 确实创建了一个原型继承的孩子 范围,这就是为什么 goaltitle(原始类型)的控制器范围与您在 ng-model 上使用的 goaltitle 不同

    理想的做法是在定义视图模型时遵循dot rule。因此,原型继承规则将遵循范围层次结构。

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

    控制器

    .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,我已经涵盖了所有详细信息。

    【讨论】:

    • 这行得通...但有一些变化... alert($scope.model.goaltitle);非常感谢... :D
    • @MansonMamaril 显然应该是这样。很高兴帮助你。谢谢:)
    • 这种方法在控制器之间有效吗?我需要将 div 固定在顶部。但是该 div 中的按钮/输入需要将数据传递给另一个控制器吗?在这种情况下我应该只使用 $rootScope 变量还是有其他方法? Ionic v1 是否有将 div 固定到屏幕顶部的方法 - 我在其他线程方法中看到了 ion-fixed 但我认为那是 Ionic 3x 及更高版本特有的。不过不确定。如果有固定 div 的 v1 方法,这一切都会容易得多。
    【解决方案2】:

    对于html

    <input type="text" placeholder="#Title" ng-model="foo.goaltitle">
    

    JS:

    $scope.foo = {{
        goaltitle : ''
    }}
    

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 2014-09-14
      • 2018-05-14
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2017-12-16
      • 1970-01-01
      相关资源
      最近更新 更多