0
我有一個plunkr用下面的代碼更新NG網與外部數據
HTML
<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
<link rel="stylesheet" type="text/css" href="http://angular-ui.github.com/ng-grid/css/ng-grid.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script type="text/javascript" src="http://angular-ui.github.com/ng-grid/lib/ng-grid.debug.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body ng-controller="MyCtrl">
<div id="myGrid" class="gridStyle" ng-grid="gridOptions"></div>
<button onclick="hi()" type="button">hi</button>
</body>
</html>
JS
var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
$scope.gridOptions = {
data: 'myData',
showFilter: true,
columnDefs: [{ field: "name", width: 120 , displayName : "Name" },
{ field: "age", width: 120 },
{ field: "birthday", width: 120 },
{ field: "salary", width: 120 }]
};
$scope.myData = [];
});
function hi()
{
scope = angular.element($("#myGrid")).scope();
console.log($("#myGrid") , angular.element($("#myGrid")) , scope);
if(scope)
scope.$apply(function(){
scope.myData = [{ name: "Moroni", age: 50, birthday: "Oct 28, 1970", salary: "60,000" },
{ name: "Tiancum", age: 43, birthday: "Feb 12, 1985", salary: "70,000" }];
})
}
現在,當我點擊按鈕嗨,我希望可以通過調用apply()和設置myData來將表格的內容設置爲我想要放置的內容。然而,它不工作..我做錯了什麼?理由:基本上,我有一個複雜的邏輯(比內置緩存功能更復雜)來檢查localStorage,然後在需要時通過REST調用檢索新數據。除此之外,它是三個REST調用的結果需要進行組合。因此,對於我來說,檢索正確的範圍,分配數據和應用()跳過大量循環之後似乎是最簡單的。
我的問題取代這個數據靜態數據真的是我怎樣才能得到正確的範圍:\ – tomdemuyt
我會盡力制定到的響應。您無法在控制器內部工作的任何具體原因?這是試圖拯救你自己的設計模式 –
更新的問題 – tomdemuyt