Javascript 在角度js的ng网格中插入行
我是新来的。 我试图在点击按钮时在ng网格中插入行。 为此,我编写了一个函数addNewItems(),用于更新json对象。 但是网格没有显示更新的对象。 这是我的密码:Javascript 在角度js的ng网格中插入行,javascript,angularjs,ng-grid,Javascript,Angularjs,Ng Grid,我是新来的。 我试图在点击按钮时在ng网格中插入行。 为此,我编写了一个函数addNewItems(),用于更新json对象。 但是网格没有显示更新的对象。 这是我的密码: <html ng-app="myApp"> <head lang="en"> <meta charset="utf-8"> <title>Getting Started With ngGrid Example</title> <link rel="styles
<html ng-app="myApp">
<head lang="en">
<meta charset="utf-8">
<title>Getting Started With ngGrid Example</title>
<link rel="stylesheet" type="text/css" href="script/ng-grid.css" />
<link rel="stylesheet" type="text/css" href="script/indexStyle.css" />
<script type="text/javascript" src="script/jquery-2.1.4.js"></script>
<script type="text/javascript" src="script/angular.min.js"></script>
<script type="text/javascript" src="script/ng-grid-1.3.2.js"></script>
<script>
var myData = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];
var app = angular.module('myApp', ['ngGrid']);
app.controller('MainCtrl', function($scope) {
$scope.gridOptions = { data: myData };
$scope.addNewItem=function()
{
myData.push( { name: 'Test add ', age:29});
$scope.gridOptions = { data: myData };
};
console.log(myData);
});
</script>
</head>
<div ng-controller="MainCtrl">
<div class="gridStyle" ng-grid="gridOptions">
<button ng-click="addNewItem()">ADD item</button>
</div>
</body>
</html>
ngGrid示例入门
var myData=[{name:“Moroni”,年龄:50},
{姓名:“Tiancum”,年龄:43},
{姓名:“雅各布”,年龄:27},
{姓名:“尼菲”,年龄:29},
{姓名:“埃诺斯”,年龄:34}];
var-app=angular.module('myApp',['ngGrid']);
应用程序控制器('MainCtrl',函数($scope){
$scope.gridOptions={data:myData};
$scope.addNewItem=函数()
{
push({name:'Test add',年龄:29});
$scope.gridOptions={data:myData};
};
console.log(myData);
});
添加项
请检查一下。
只需从js中删除以下代码:
$scope.gridOptions = { data: myData };
你可以试试这个
app.controller('MainCtrl', function($scope) {
$scope.data = myData;
$scope.gridOptions = { data: 'data' };
...
试试这个
app.controller('MainCtrl', function($scope) {
$scope.gridOptions = { data: myData };
$scope.addNewItem = function() {
$scope.gridOptions.data.push({ name: 'Test add ', age: 29 });
};
console.log($scope.gridOptions.data);
});
嘿,谢谢@Gurkan yesilyart,@anis程序员,@Gaurav,@kinjal jethva 这样做后,它开始工作:
var myData = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];
var app = angular.module('myApp', ['ngGrid']);
app.controller('MainCtrl', function($scope) {
$scope.data = myData;
$scope.gridOptions = { data: 'data' };
$scope.addNewItem = function() {
$scope.data.push({ name: 'Test add ', age: 29 });
};
console.log($scope.gridOptions.data);
});