离子框架:$scope在简单警报中未定义
Ionic Framework: $scope is undefined in simple alert
.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((;
简答题
此问题的根本原因是,
ion-content
确实创建了一个原型继承的子项 作用域,这就是为什么控制器作用域的goaltitle
(基元类型(与您在ng-model
上使用的goaltitle
不同的原因
理想情况下,在定义视图模型时遵循dot rule
。因此,原型继承规则将遵循范围层次结构。
您应该定义对象,然后分配其中的所有ng-model
属性。
控制器
.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>
我不想再次重写整个解释,所以在这里我引用类似的答案,我已经涵盖了所有详细信息。
对于 html
<input type="text" placeholder="#Title" ng-model="foo.goaltitle">
.JS:
$scope.foo = {{
goaltitle : ''
}}
相关文章:
- JS方法显示了未定义的简单示例
- *简单*解析云代码查询失败”;TypeError:无法调用方法'获取'未定义的“检索该信息”;
- 离子框架:$scope在简单警报中未定义
- 使用参数成功调用简单函数后出现意外警报,消息为“未定义”
- 无法读取简单表的未定义的属性“mData”
- 为具有未定义值的嵌套对象设置默认值的最简单方法
- 为什么这个带有最简单的构造函数的 javascript 对象未定义
- 在简单示例中未处理和未定义 Enyo 自定义事件
- 返回未定义的javascript简单测试
- 未捕获的ReferenceError:没有为简单按钮定义startPause
- 简单Javascript程序:未定义Uncaught ReferenceError:x
- 返回简单数组时出现问题:;未捕获的ReferenceError:n未定义”;
- 简单的jquery+javascript不起作用:add".有错误”;不工作,并且未定义onclick事件
- 未捕获的ReferenceError: $是未定义的错误在简单的网页上收到
- 简单的javascript函数未定义错误
- 不能调用'toUpperCase'在简单的React/Jest测试中未定义
- 简单JSON保持未定义状态
- 寻找一种更简单的方法来检查是否多个属性&对象中的方法是未定义的
- Sencha Touch 2:一个简单的处理HTML按钮显示在面板内给出未定义的错误
- 无法读取属性'力'未定义的(简单D3网络图)