2014-07-17 61 views
0

我,我在角JS我节省一个JSON清单得分VAR角JS数据推入表不工作

$scope.jobTemplate = [{ type: "AddInstructions", visible: false, buttonText: "Add Instructions", editableInstructionsList: [{ Number: totalEditableInstruction, Text: "Instruction 1"}] }, 
    { type: "AddSingleQuestionsList", visible: false, buttonText: "Add Ques. (single Ans.)", singleQuestionsList: [{ Number: totalSingleQuestionList, Question: "What is your gender ?", Options: "Male1;Female2"}] }, 
    { type: "AddMultipleQuestionsList", visible: false, buttonText: "Add Ques. (Multiple Ans.)", multipleQuestionsList: [{ Number: totalMultipleQuestionList, Question: "What is your multiple gender ?", Options: "Malem1;Femalem2"}] }, 
    { type: "AddTextBoxQuestionsList", visible: false, buttonText: "Add Ques. (TextBox Ans.)", textBoxQuestionsList: [{ Number: totalTextBoxQuestionList, Question: "Who won 2014 FIFA World cup ?", Options: "text"}] }, 
    { type: "AddListBoxQuestionsList", visible: false, buttonText: "Add Ques. (ListBox Ans.)", listBoxQuestionsList: [{ Number: totalListBoxQuestionList, Question: "What is your multiple gender ?", Options: "Malem1;Femalem2"}] } 
]; 

我推按钮的点击数据如下

// single questions.. 
$scope.InsertSingleQuestionRow = function() { 
    totalSingleQuestionList = totalSingleQuestionList + 1; 
    var singleQuestionsList = { Number: totalSingleQuestionList, Question: $('#SingleQuestionTextBoxQuestionData').val(), Options: $('#SingleQuestionTextBoxAnswerData').val() }; 
    $scope.jobTemplate[1].singleQuestionsList.push(singleQuestionsList); 
    refreshSingleQuestionsList(); 
} 

虽然在用户界面中新添加的项目正常显示,但是当我试图通过http post发送当前范围变量数据到服务器时,它没有最新的数据。

$scope.ClientCreateTemplateFunction = function() { 
    var clientCreateTemplateData = $scope.jobTemplate;   
    var url = ServerContextPah + '/Client/CreateTemplate';   
    if (true) { 
     //startBlockUI('wait..', 3); 
     $http({ 
      url: url, 
      method: "POST", 
      data: clientCreateTemplateData, 
      headers: { 'Content-Type': 'application/json' } 
     }).success(function (data, status, headers, config) { 
      //$scope.persons = data; // assign $scope.persons here as promise is resolved here 
      //stopBlockUI(); 

     }).error(function (data, status, headers, config) { 

     }); 
    } 
    else { 
     $scope.showErrors = true; 
     showToastMessage("Error", "Some Fields are Invalid !!!"); 
    } 

} 

我也尝试过全局变量。我不知道为什么范围变量工作正常与用户界面,而不是工作时,通过http发送到服务器相同的数据。 帮助我。

我已经附加了一个快照,其中UI显示两个列表,而当我console.log相同的范围变量它只显示一个列表。

enter image description here

+0

你说的'不具有的意思最新数据“?你是否在发起http请求之前设置了一个断点,并发现$ scope.jobTemplate不是最新的? – runTarm

+0

我已经上传了上传的快照。 –

+0

如果您添加了第三项,会发生什么情况? – runTarm

回答

2
$scope.ClientCreateTemplateFunction = function() { 
    var clientCreateTemplateData = $scope.jobTemplate; 
    var url = ServerContextPah + '/Client/CreateTemplate'; 
    if (true) { 
    //startBlockUI('wait..', 3); 
    $http.post(url, $scope.jobTemplate).then(onSuccess, onError); 

    function onSuccess(data, status, headers, config) { 
     //$scope.persons = data; // assign $scope.persons here as promise is resolved here 
     //stopBlockUI(); 
    }; 

    function onError(data, status, headers, config) {}; 

    } else { 
    $scope.showErrors = true; 
    showToastMessage("Error", "Some Fields are Invalid !!!"); 
    } 

} 
+0

仍然有相同的问题:( –

+0

你可以请创建plunker或jsfiddle? – sylwester

1

你不能在发送一个javascript自定义对象POST方法使用jQuery的param到对象转换为字符串或转换到JSON

$http({ 
      url: url, 
      method: "POST", 
      data: $.param(clientCreateTemplateData, true); 
      headers: { 'Content-Type': 'application/json' } 
     }).success(function (data, status, headers, config) { 
      //$scope.persons = data; // assign $scope.persons here as promise is resolved here 
      //stopBlockUI(); 

     }).error(function (data, status, headers, config) { 

     }); 
+1

从[manual](https://docs.angularjs.org/api/ng/service/$http):*默认情况下,Angular应用这些转换:...如果请求配置对象的数据属性包含对象,将其序列化为JSON格式。*因此,不需要或不推荐jquery。 – Yoshi