[英]Not getting data in AngularJS Directive from the $scope model
我正在使用AngularJS開發網頁。 在網頁上,我正在使用Chartist.js顯示圖表。 我想通過調用返回JSON數據並將其提供給Chartist圖表數據模型的REST服務來填充Chartist圖表對象所需的數據。 該網頁僅在面板內顯示圖表。 我有一個稱為REST服務的Angular控制器,獲取了數據,然后將其分配給$ scope模型。 我想將此模型數據傳遞給Chartist對象。 為此,我將一條指令應用於元素,並將模型數據作為屬性值傳遞,並在該指令中獲取此數據,創建一個Chartist對象並傳遞此模型數據。 但是,該圖表未顯示,表示沒有/空數據傳遞到Chartist對象。 我在這里做錯了! ?...這是html文件
<!DOCTYPE html>
<html ng-app="codeQualityApp">
<head>
<title>First Test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="lib/angularjs/angular.js"></script>
<!--<script src="controllers/client.controller.codequality.js"></script>-->
<link href="css/bootstrap.css" rel="stylesheet" />
<link href="css/bootstrap-theme.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css" rel="stylesheet" />
<script>
angular.module("codeQualityApp",[])
.controller('codeQualityCtrl',function($scope,$http)
{
$scope.violationsData = {}; // Model data in the format below as needed by the Chartist object
// $scope.violationsData.labels = ["Clang","MySQL","JDK 8"];
//$scope.violationsData.series= [[369492,167703,159215]];
$http.get("http://localhost:5001rest/codequality/list") // REST call
.success(function(data,status,header,config)
{
var cq_violations = angular.fromJson(data);
violationsData = {};
violationsData.labels = new Array();
violationsData.series = new Array();
violationsData.series[0] = new Array();
for(var i =0; i < cq_violations.length; i++)
{
violationsData.labels[i] = cq_violations[i].name;
violationsData.series[0][i] = parseInt(cq_violations[i].msr[0].val,10);
}
$scope.violationsData = violationsData; // Populating the model data
})
.error(function(data,status,header,config)
{
$scope.error = data;
});
})
.directive("cqChart",function()
{
return function(scope,element,attrs)
{
chartdata = scope[attrs["chartdata"]]; // Accessing the attribute value
console.log("ChartData:",chartdata); // Am getting empty object here!!!
new Chartist.Bar('.ct-chart', chartdata); // Bar chart with the data as in chartdata
// but chart not displayed as chartdata is empty
// object!!
}
});
</script>
</head>
<body ng-controller="codeQualityCtrl"> // Controller
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Dashboard</a>
</div>
<div>
<ul class="nav navbar-nav">
<li><a href="master.html">Master</a></li>
<li><a href="#">Project/Features</a></li>
<li><a href="build.html">Build</a></li>
<li class="active"><a href="#">Code Quality</a></li>
<li><a href="#">Test Execution</a></li>
<li><a href="#">Deployments</a></li>
</ul>
</div>
</div>
</nav>
<div class="alert alert-danger" ng-show="error">
Error ({{error}}). CodeQuality data was not loaded.
<a href="/app.html" class="alert-link">Click here to try again</a>
</div>
<div class="panel panel-default" ng-hide="error">
Data : {{violationsData}} <!-- Got proper data here -->
<!-- Display the chart here -->
<script src="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
<div class="ct-chart ct-perfect-fourth"></div>
<cq-chart chartdata="violationsData"> <!-- custom directive ... not working... data is empty -->
</div>
</body>
</html>
訪問值的方式始終是獲取未更新的初始化值。您發現它為空,因為那是初始值$scope.violationsData = {};
在ajax成功之后,您的值將更新。 更新后的值不會傳播到您的指令。 因此,要獲取更新的值,您可以$watch
或使用=
like傳遞值
.directive("cqChart", function () {
return {
restrict: 'E',
scope: {
// this will create isolate scope and two way bind between
// your controller's violationsData and directive's chartdata
chartdata: '='
},
link: function (scope, element, attrs) {
//now this will be accessable through scope
console.log("ChartData:", scope.chartdata);
}
}
});
注意 -不要使用something = someValue;
像var something = someValue
一樣使用var,否則將創建全局變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.