以角度添加到列表功能
Add to list functionality in angular
我是 angular 的新手,并试图实现添加到列表功能。我有几个问题
- 为什么
$scope.newChat
的console.log
返回未定义 - 由于吊装可变,
newChat
可用于sendChat()
呼叫。
模板
<ion-list>
<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="chat in chats" type="item-text-wrap" href="#/tab/chats/{{chat.id}}">
<img ng-src="{{chat.face}}" >
<h2>{{chat.name}}</h2>
<p>{{chat.lastText}}</p>
<i class="icon ion-chevron-right icon-accessory"></i>
<ion-option-button class="button-assertive" ng-click="remove(chat)">
Delete
</ion-option-button>
</ion-item>
<ion-item >
<form ng-submit="sendChat(newchat)"> <!-- this line in question 2 -->
<label class="item item-input">
<input type="text" placeholder="What do you need to do?" ng-model="newchat.lastText">
<button type="submit" class="button button-right button-positive">Send</button>
</label>
</div>
</form>
</ion-item>
</ion-list>
控制器
.controller('ChatsCtrl', function($scope, Chats) {
$scope.chats = Chats.all();
$scope.remove = function(chat) {
Chats.remove(chat);
}
$scope.sendChat = function(newchat){
Chats.set(newchat);
console.log($scope.newchat); //this line in question 1
newchat.lastText = "";
}
})
1)为什么$scope.newChat的控制台.log返回未定义
您在作用域上获得 NewChat console.log($scope.newchat);
尝试使用控制台日志记录console.log(newchat);
它们都与 ng-model 中的 Newchat 相同,使其在作用域中可用。在下面的演示中单击发送按钮后查看控制台
2)newChat 是否可用于 sendChat() 调用,因为变量提升。
否,由于ng模型数据绑定,它可用
演示
angular.module('myApp',[])
.controller('ChatsCtrl', function($scope) {
//$scope.chats = Chats.all();
$scope.remove = function(chat) {
//Chats.remove(chat);
}
$scope.sendChat = function(newchat){
// Chats.set(newchat);
console.log($scope.newchat); //this line in question 1
newchat.lastText = "";
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ChatsCtrl"> <form ng-submit="sendChat(newchat)"> <!-- this line in question 2 -->
<label class="item item-input">
<input type="text" placeholder="What do you need to do?" ng-model="newchat.lastText">
<button type="submit" class="button button-right button-positive">Send</button>
</label>
</form>
</div>
将
控制器更改为以下内容
.controller('ChatsCtrl', function($scope, Chats) {
$scope.newchat = {};
$scope.chats = Chats.all();
$scope.remove = function(chat) {
Chats.remove(chat);
}
$scope.sendChat = function(newchat){
Chats.set(newchat);
console.log($scope.newchat); //now you will have your $scope.newchat
newchat.lastText = "";
}
})
相关文章:
- 简化了点击功能的长列表
- 如何创建一个脚本,该脚本给定网站列表,它使用其搜索功能来获取信息
- 好友列表功能
- 谷歌类功能的猜测列表
- 更改选择选项下拉列表与onclick功能按钮
- 在选择某些项目时显示文本和列表的功能
- 如何在输入下从数据列表中选择一个选项后立即触发功能
- 如何在WinJS中为列表视图创建排序功能
- 如何在Spring MVC中实现无序列表的展开/折叠功能
- Javascript - 更快的功能:线性列表搜索或获取对象值
- 项目点击和收听事件功能 煎茶触摸 2 列表
- Google Place 自动完成功能会在下拉列表中的结果(预测)中显示“street_number”,但不会在 .ge
- 我的播放器播放列表删除功能不起作用
- “功能上”生成一个仅包含几个元素的列表
- 以角度添加到列表功能
- 在引导程序 3 按钮下拉列表中添加滑动功能时出现问题
- 使用Pusher/Pubnub创建好友列表功能
- 如何使用windows phone 8 c#中的jQuery列表功能
- 移动列表功能无法正常工作
- 我怎样才能得到一个按"减少"的降序排序的映射/减少结果;值";价值如果也使用列表功能可以实