Javascript $scope.data.push进入json

Javascript $scope.data.push进入json,javascript,json,angularjs,push,Javascript,Json,Angularjs,Push,我有一个表单和一个json文件。我希望将输入的数据推送到表单元素中,并在主数组中创建新的json对象 <html ng-app="example"> <div ng-controller="ListController"> <form name="myform" ng-submit="addThis()"> <input type="text" ng-model="model1">

我有一个表单和一个json文件。我希望将输入的数据推送到表单元素中,并在主数组中创建新的json对象

<html ng-app="example">
    <div ng-controller="ListController">
        <form name="myform" ng-submit="addThis()">
            <input type="text" ng-model="model1">
            <input type="text" ng-model="model2">
        </form>
    </div>
</html>
我做错了什么?

有几件事

在你的js中,它喜欢 1) 您没有正确关闭其中一个功能

2) 您没有正确定义范围变量,$scope(据我所知)不存储html对象的“name”属性。您可以使用花括号或某些ng标记(如ng模型)通过html引用存储在控制器作用域中的变量

3) 如果要将变量存储在$scope.mydata.somevar中,请确保从mydata.somevar调用它们

JS

var-app=angular.module('example',[]);
app.controller('ListController',[“$scope”,“$http”,函数($scope){
$scope.mydata={model1:'',model2:''};//先前初始化模型(良好实践)
$http.get(“js/js/data.json”).success(函数(数据){
$scope.mydata.data=数据;

});//非常感谢您提供的提示和更正。但是,我仍然无法使其正常工作。我认为这可能与我的软件的整个体系结构有关。我使用了ng include很多,这意味着文件自动存储信息,我觉得这可能是问题的核心。
 var app = angular.module('example', []);
 app.controller('ListController', ["$scope", "$http", function($scope) {
     $http.get("_/js/data.json").success(function(data) {
         $scope.mydata = data;
     });
     $scope.addThis = function() {
         $scope.mydata.push({
              model1:$scope.mydata.keyname1,
              model2:$scope.mydata.keyname2
         });
     };
 }]);
var app = angular.module('example', []);
app.controller('ListController', ["$scope", "$http", function($scope) { 

 $scope.mydata = { model1: '', model2: ''} ;// initializing the model earlier on (good practice) 

 $http.get("_/js/data.json").success(function(data) { 
    $scope.mydata.data = data; 
 }); //<----- forgot a bracket here! 

 $scope.addThis = function() {  
   $scope.mydata.model1=$scope.mydata.data.keyname1;     
   $scope.mydata.model2 =$scope.mydata.data.keyname2;
 }; 
}]);
<html ng-app="example">
     <div ng-controller="ListController">     
         <form name="myform" ng-submit="addThis()">
            <input type="text" ng-model="mydata.model1"> 
            <input type="text" ng-model="mydata.model2"> <!--- notice the mydata.model2!! ---->
          </form>
      </div> 
 </html>