无法更改数组的数据
unable to change data of an array
这是一些与javascript相关的问题(不是角度,但上下文是有角度的),试图整理几个小时没有任何成功。
我正在将内容修剪为较短的段落,并将子内容分配回数组的内容属性,但显示的仍然是较大的内容。
controller: function($scope, $location) {
this.$scope = $scope;
this.$scope.folio = {
"deleted" : null,
"free" : null,
"id" : null,
"lastModified" : null,
"productId" : null,
"publication" : null,
articles: []
};
},
result: function(folio) {
this.$scope.folio.articles.length = 0;
this.$scope.folio.deleted = folio.deleted
this.$scope.folio.free = folio.free
this.$scope.folio.id = folio.id
this.$scope.folio.lastModified = folio.lastModified
this.$scope.folio.productId = folio.productId
this.$scope.folio.publication = folio.publication
for(var i=0; i<folio.articles.length; i++) {
for(var j=0; j<folio.articles[i].pages.length; j++) {
var content = folio.articles[i].pages[j].content
var index = content.toLowerCase().indexOf("test".toLowerCase())
if(index > 150) { //if index is not within first 150 characters
content = content.substring(index - 25, index + 30)
}
//displays fine with the desired output
console.log(content.replace(new RegExp("test", "gi"), "<em>" + "test" + "</em>"))
//assigning back to the array but for some reason it's not the trimmed output at the end
folio.articles[i].pages[j].content = content.replace(new RegExp("test", "gi"), "<em>" + "test" + "</em>")
}
//the article's pages are still full length
this.$scope.folio.articles.push(folio.articles[i]);
}
this.$scope.$apply();
}
这应该可以帮助您更新内容。
(function(angular){
var myApp = angular.module('myApp');
myApp.controller('Controller', function(){
$scope.folio = {
"deleted" : null,
"free" : null,
"id" : null,
"lastModified" : null,
"productId" : null,
"publication" : null,
articles: []
};
$scope.result = function(folio) {
$scope.folio = folio;
angular.forEach($scope.folio.articles, function(article, articleIndex)) {
angular.forEach(article.pages, function(page, pageIndex)) {
var content = page.content,
index = content.toLowerCase().indexOf("test".toLowerCase());
if(index > 150) { //if index is not within first 150 characters
content = content.substring(index - 25, index + 30)
}
//displays fine with the desired output
console.log(content.replace(new RegExp("test", "gi"), "<em>" + "test" + "</em>"))
//assigning back to the array but for some reason it's not the trimmed output at the end
$scope.folio.articles[i].pages[j].content = content.replace(new RegExp("test", "gi"), "<em>" + "test" + "</em>")
}
$scope.folio.articles.push(folio.articles[i]);
}
$scope.$apply();
}
});
})(window.angular);
不要忘记在HTML中使用ng-app="myApp"
和ng-controller="ControllerName"
。
相关文章:
- 使用数据数组创建多个类似组件
- WebAudio API数据数组大小
- Jquery 循环一次或在数据数组中显示一次数据
- Cakephp 访问不在模型数据数组上的输入
- 使用来自 Ajax 响应的 JSON 数据数组
- Angular JS(离子)数据数组到$scope元素
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 如何使用for循环语句通过Ajax发送大数据数组
- 压缩 JS 中的数据数组
- 传递的参数D3提示回调是完整的数据数组
- 如何在数据数组的每次迭代中创建一个内容为“i+1”的新 HTML 元素
- 在 laravel 4 上使用循环将数据数组存储到单个数组中
- 为什么它说输入数据数组的格式不正确 jqchart.
- Javascript - 使用变量 RegExp 匹配数据数组中的多个关键字
- 尝试将数据数组设置为从视图调用函数后$scope
- 如何在 PHP 中创建 jqplot 数据数组
- 如何使用该数据数组从左向右移动画布
- 如何根据数字列从 Parse 中对数据数组进行排序
- 为什么 php json 响应不会将数据数组发送回 ajax jquery 请求
- 如何将内容添加到 JQuery Flot 图表数据数组工具提示