我有短语:
我想获取短语中的所有最后一个字符:
AngularJs 中的“Jolie”、“Novel”、“Bat”、“Jcalie”。
你能帮我吗?
谢谢!
希望这是您正在寻找的。我添加了一个附加函数,返回
phrase
的最后一个单词
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.phrases = ["Shiloh Match Jolie", "Zahara Marley Novel", "Pax Dix Bat", "Maddox Chivan Jcalie"];
// Returns the last word of a phrase
$scope.getLastWord = function (phrase) {
var words = phrase.trim().split(' ');
return words[words.length - 1];
};
// Returns the last character of a phrase
$scope.getLastCharacter = function (phrase) {
// Assuming the phrase is not empty
return phrase.trim().slice(-1);
};
});
<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="phrase in phrases">
Last Word in the Pharse : {{ getLastWord(phrase) }}
Last Character in the Last Word of the Pharse: {{ getLastCharacter(phrase) }},
</li>
</ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>