小编典典

离子框架:$ scope在简单警报中未定义

angularjs

.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 =“东西”正在.controller();上工作。…


阅读 248

收藏
2020-07-04

共1个答案

小编典典

简短答案

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

理想的做法是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>

我不想再次重写整个说明,因此在这里我引用的是类似的答案,其中涵盖了所有详细信息。

2020-07-04