Javascript AngularJS语句和字数的双向数据绑定
在一家公司的面试过程中,我被分配去解决一个问题,除了字数部分外,我都做了。这导致进一步面试过程被拒绝 现在我试着数数单词的数量,但不知道我做得对不对。因此,请建议我根据给定的问题计算AngularJs中单词数量的最佳方法 多谢各位Javascript AngularJS语句和字数的双向数据绑定,javascript,angularjs,word-count,plunker,Javascript,Angularjs,Word Count,Plunker,在一家公司的面试过程中,我被分配去解决一个问题,除了字数部分外,我都做了。这导致进一步面试过程被拒绝 现在我试着数数单词的数量,但不知道我做得对不对。因此,请建议我根据给定的问题计算AngularJs中单词数量的最佳方法 多谢各位 <!DOCTYPE html> <html ng-app="plunker"> <head> <title>Agenus</title> <link rel="stylesheet" hre
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<title>Agenus</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div ng-controller="MainController as mainVm">
<div class="container">
<div id="heading">
<h3 id="header">Photo Data from JSON Endpoint</h3>
</div>
<div class="row">
<div class="col-sm-9">
<table class="table">
<thead>
</thead>
<tbody>
<tr ng-repeat="photo in mainVm.photos" id="trow">
<td><img src={{photo.url}} class="img-responsive" alt="imgg" width="300" height="300"></td>
<td>
<div id="count">{{photo.title.length}}</div>
</td>
<td id="elem">{{photo.title}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="https://code.angularjs.org/1.4.9/angular.js"></script>
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<style type="text/css">
.container {
background-color: #F5F5F5;
padding-top: 25px;
}
#heading {
margin: 0 auto;
text-align: left;
background-color: #9E9E9E;
padding-top: 25px;
padding-bottom: 25px;
border-radius: 10px;
width: 850px;
float: left;
}
#header {
text-align: left;
padding-left: 20px;
color: #FAFAFA;
}
#count {
border: 1px solid black;
border-radius: 5px;
background-color: #E0F2F1;
}
#trow {
background-color: #FFFFFF;
}
</style>
<script>
(function() {
'use strict';
angular
.module('plunker', [])
.controller('MainController', MainController);
function MainController($http) {
var mainVm = this;
var query = {
albumId: 1
};
$http({
method: 'GET',
url: 'http://jsonplaceholder.typicode.com/photos',
params: query
})
.then(successFn, errorFn);
function successFn(response) {
mainVm.photos = response.data;
}
function errorFn(response) {
console.log(response.status);
}
}
})();
</script>
</body>
</html>
阿根努斯
来自JSON端点的照片数据
{{photo.title.length}
{{photo.title}
.集装箱{
背景色:#F5;
填充顶部:25px;
}
#标题{
保证金:0自动;
文本对齐:左对齐;
背景色:#9E9E9E;
填充顶部:25px;
垫底:25px;
边界半径:10px;
宽度:850px;
浮动:左;
}
#标题{
文本对齐:左对齐;
左侧填充:20px;
颜色:#FAFAFA;
}
#计数{
边框:1px纯黑;
边界半径:5px;
背景色:#E0F2F1;
}
#特罗{
背景色:#FFFFFF;
}
(功能(){
"严格使用",;
有棱角的
.module('plunker',[])
.控制器(“主控制器”,主控制器);
函数MainController($http){
var mainVm=这个;
变量查询={
专辑编号:1
};
$http({
方法:“GET”,
网址:'http://jsonplaceholder.typicode.com/photos',
参数:查询
})
.然后(成功fn,错误fn);
功能成功fn(响应){
mainVm.photos=response.data;
}
功能错误fn(响应){
console.log(响应状态);
}
}
})();
添加带有单个空格参数的split方法将把字符串拆分为一个单词数组。从那里,问长度,你会有一个字数
以下是检索此文件的几种方法之一:
<td>{{user.title.length}}</td>
<td>{{user.title.split(" ").length}}</td> //Add this line
<td>{{user.title}}</td>
{{user.title.length}
{{user.title.split(“”.length}}//添加此行
{{user.title}}
我想强调的是,这不一定是“正确”的做法。但考虑到应用程序的当前结构,这是最简单的方法