Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Knockout.js 可观测数组中对象的更新已完成,但浏览器上不会出现更改_Knockout.js - Fatal编程技术网

Knockout.js 可观测数组中对象的更新已完成,但浏览器上不会出现更改

Knockout.js 可观测数组中对象的更新已完成,但浏览器上不会出现更改,knockout.js,Knockout.js,我现在面临一个问题。我有一个包含对象列表的可观察数组。每当我更新数组中任何对象的属性时,它都不会反映在浏览器上。我使用了所有的删除功能,如替换、删除。但是更新是在可观察数组中进行的,而不是在浏览器中 以下是我发行的一个样本: var ViewModel=new { self=this; self.List=ko.observableArray([]); } $(function(){ ko.applyBinding(Vi

我现在面临一个问题。我有一个包含对象列表的可观察数组。每当我更新数组中任何对象的属性时,它都不会反映在浏览器上。我使用了所有的删除功能,如替换、删除。但是更新是在可观察数组中进行的,而不是在浏览器中

以下是我发行的一个样本:

 var ViewModel=new {
     self=this;
     self.List=ko.observableArray([]); 
              }
     $(function(){
       ko.applyBinding(ViewModel); 
    }) 
    $.post('/url',{},function(data){
        ViewModel.List(data); //data is list having 4 property having CommentList as again object-->id,title,description,CommentList--->commenttitle,commentdescription
      })  
    //During change of property of commentList
    $.post('/updateComment',{},function(obj){//Here obj-->obj.Commenttitle="some title",obj.commentdescription='some description'
       //Let say there require update 4th object of List and  2nd property of CommentList
         ViewModel.AnswerList()[4].CommentList.splice(2,1,obj);

     })
    //But nothing updation on browser
你说:

每当我更新数组的任何对象的属性时,它都不是 反映在浏览器上

observable数组中对象的属性也需要设置为
ko.observable
,以便用户界面自动更新

例如:

var anObservableArray = ko.observableArray([    
  { name: "A", type: "Type A" }    
]);

// some later point
anObservableArray()[0].name = "B";
不会更新您的UI,因为
名称
不可见

但是,

var anObservableArray = ko.observableArray([    
  { name: ko.observable("A"), type: ko.observable("Type A") }    
]);

// some later point
anObservableArray()[0].name("B");
…将更新您的UI以显示名称B,因为
name
是可观察的

编辑:(将代码添加到问题后)

因此,从您的代码可以看出:

 answer=GetAnswerFromViewModel(parentcourseItemID); 
 answer.CommentCount--;                     
 answer.CommentList.splice(CommentIndex,1); 
 answer.CommentText=''; 
假设
GetAnswerFromViewModel
返回一个带有可观察属性的答案,您应该写:

 answer=GetAnswerFromViewModel(parentcourseItemID); 
 answer.CommentCount(answer.CommentCount()--);                     
 answer.CommentList.splice(CommentIndex,1); 
 answer.CommentText(''); 

如果你的答案的属性不是可观察的,那么你的UI就不会更新。

请考虑将你的文章编辑成精确的语言,而你的问题是一些最小的问题,即数组是面向服务器端的。目前还不清楚到底是什么问题。如果您有来自服务器的数据,那么我建议您查看映射插件,它将JSON数据(例如,您的数组)转换为可观察的属性。实际上,在映射中,它是直接将数据传递给ObserverRay的。但在我的例子中,我从服务器端获取了需要在数组上更新的对象,如果我们将代码编写为var listObj=ViewModel.AnswerList(),它还需要在ObserverRay中更新对象之后更新UI;ViewModel.Answer([]);ViewModel.Answer(listobj);然后它会更新UI,否则不会。但这是不可取的。