[AngularJS] Transforming raw JSON data to meaningful output in AngularJS
angular.module('APP', []) .controller('MainController', function($scope, UserConstants){
var user = {
firstName: 'Benjamin',
lastName: 'Roth'
};
this.user = user;
this.userTypes = UserConstants.types;
})
.service('UserPresenter', function(UserConstants){
var typeFromId = _.memoize(function(typeId){
var obj = _.findWhere(UserConstants.types, { value: typeId});
return obj ? obj.display : '';
}); return {
fullName: function(user){
return user.firstName + ' ' + user.lastName;
},
type: function(user){
return typeFromId(user.typeId);
}
};
})
.factory('UserModel', function(UserPresenter){
function UserModel(props){
_.extend(this, props);
}
UserModel.prototype.fullName = function(){
return UserPresenter.fullName(this);
};
UserModel.prototype.type = function(){
return UserPresenter.type(this);
};
return UserModel;
})
.filter('user', function(UserPresenter){
return function(user, fnName){
return UserPresenter[fnName](user);
};
})
.constant('UserConstants', {
types: [
{ value: '1', display: 'Front end' },
{ value: '2', display: 'Back end' },
{ value: '3', display: 'Full stack' }
]
});
<!DOCTYPE html>
<html ng-app="APP">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js">
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="container-fluid" ng-controller="MainController as main">
<div class="row">
<div class="col-sm-3">
<pre>{{ main.user | json}}</pre>
</div>
<hr>
<div class="col-sm-3">
<form>
<div class="form-group">
<label>First Name</label>
<input ng-model="main.user.firstName" type="text">
</div>
<div class="form-group">
<label>Last Name</label>
<input ng-model="main.user.lastName" type="text">
</div>
<div class="form-group">
<label>Type</label>
<select ng-model="main.user.typeId" ng-options="type.value as type.display for type in main.userTypes"></select>
</div>
</form>
</div>
<hr>
<div class="col-sm-3">
<form>
<div class="form-group">
<label>Full Name</label>
{{ main.user | user:'fullName' }}
</div>
<div class="form-group">
<label>Type:</label>
{{ main.user | user:'type' }}
</div>
</form>
</div>
</div>
</div>
</body>
</html>
[AngularJS] Transforming raw JSON data to meaningful output in AngularJS的更多相关文章
- SQL to JSON Data Modeling with Hackolade
Review: SQL to JSON data modeling First, let’s review, the main way to represent relations in a rela ...
- directly receive json data from javascript in mvc
if you send json data to mvc,how can you receive them and parse them more simply? you can do it like ...
- Guzzle Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
项目更新到正式平台时,出现Guzzle(5.3) client get请求出现:Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, ...
- AssetsUtils【读取assets、res/raw、./data/data/包名/目录下的文件】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装了以下功能: 1.读取assets目录下的资源html.文件.图片,将文件复制到SD卡目录中: 2.读取res/raw目录下的文 ...
- POST Raw JSON提交
contentType: "application/json", data: JSON.stringify( { org: org, worksite_info: info }), ...
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决
记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data
JSON.parse转化Json字符串时出现:SyntaxError: JSON.parse: bad control character in string literal at line 1 co ...
- PHP convet class to json data
/********************************************************************* * PHP convet class to json da ...
- json data 解析demo
json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data&quo ...
随机推荐
- Android 属性动画(一)
1.概述 Android提供了几种动画类型:View Animation .Drawable Animation .Property Animation .View Animation相当简单,不过只 ...
- 自定义PopupWindow动画效果
public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...
- .NET基础拾遗(7)多线程开发基础3
一.如何使用异步模式? 异步模式是在处理流类型时经常采用的一种方式,其应用的领域相当广阔,包括读写文件.网络传输.读写数据库,甚至可以采用异步模式来做任何计算工作.相对于手动编写线程代码,异步模式是一 ...
- Android -------- 序列化器生成xml文件
- displaytag 简单使用流程
1. 首先导入包:displaytag-1.2.jar,commons-lang.jar和standard.jar;commons-beanutils.jar;这四个jar包 2. 然后在jsp页面做 ...
- iOS 点击cell下拉
iOS 点击cell下拉 代码如下: #import "ViewController.h" @interface ViewController ()<UITableView ...
- Kendo Web UI Grid添加一个html控件如(checkbox,button)
在Kendo Web UI Grid增加一个控件如效果图: <div id="grid1"></div><script> $("#gr ...
- php 图片上传预览(转)
网上找的图片上传预览: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- DESTOON系统文章模块默认设置第一张图片为标题图的方法
连上FTP或者其他方法打开网站目录下的\module\article\admin\template\edit.tpl.php修改设置内容第 <input name="post[thum ...
- 使用redis缓存加索引处理数据库百万级并发
使用redis缓存加索引处理数据库百万级并发 前言:事先说明:在实际应用中这种做法设计需要各位读者自己设计,本文只提供一种思想.准备工作:安装后本地数redis服务器,使用mysql数据库,事先插入1 ...