2016-06-15 3 views
1

회사의 인터뷰 과정에서 나는 단어 개수 부분을 제외하고는 coding exercise을 해결하도록 지정되었습니다. 이로 인해 추가 인터뷰 프로세스가 거부되었습니다.문장과 단어 개수의 AngularJS 양방향 데이터 바인딩

이제 단어 수를 세지 만 제대로하고 있는지 잘 모릅니다. 그러니 주어진 문제에 따라 AngularJs의 단어 수를 세는 가장 좋은 방법을 제안 해주십시오.

Output of my code

My code plunkr

<!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> 

답변

3

단어의 배열로 문자를 당신의 문자열을 중단하는 단일 공백의 인수와 분할 방법을 추가 감사드립니다. 거기에서 길이를 물어 보면 단어 수를 얻게됩니다. 나는이 그것을 할 수있는 "올바른"방법은 반드시 아니라는 것을 강조하고 싶어

<td>{{user.title.length}}</td> 
<td>{{user.title.split(" ").length}}</td> //Add this line 
<td>{{user.title}}</td> 

: 아래

이 검색을위한 여러 가지 방법 중 하나입니다. 그러나 이것은 응용 프로그램의 현재 구조를 고려하는 가장 쉬운 방법입니다.

+1

감사합니다. – SumitHusky

관련 문제