.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 whygoaltitle
(primitive type) of controller scope is different than thegoaltitle
you are using onng-model
理想的做法是在定义 View 模型时遵循点规则
。因此,范围层次结构将遵循原型(prototype)继承规则。
您应该定义对象,然后分配其中的所有 ng-model
属性。
Controller
.controller('newGoalCtrl', function($scope, $ionicPopup) {
$scope.model = {};
$scope.addNewGoal = function() {
alert($scope.model.goaltitle);
};
});
然后在其中添加goalTitle
、Goal
等属性。
标记
<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/