我在angularJS应用程序中有两个自定义指令.一个充当父母,另一个充当孩子.我试图访问子指令内的父范围.但我没有得到所需的输出.
<div ng-controller="CountryCtrl">
{{myName}}
<div ng-controller="StateCtrl">
<state nameofthestate="'Tamilnadu'">
<city nameofthecity="'Chennai'"></city>
</state>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我的剧本看起来像
var app = angular.module("sampleApp",[]);
app.controller("CountryCtrl",function($scope){
$scope.myName = "India";
});
app.controller("StateCtrl",function($scope){
});
app.directive("state",function(){return {
restrict : 'E',
transclude: true,
scope : { myName : '=nameofthestate'},
template:"** {{myName}} is inside {{$parent.myName}}<br/><ng-transclude></ng-transclude>"
}});
app.directive("city",function(){return {
restrict : 'E',
require:'^state',
scope : { myName : '=nameofthecity'},
template:"**** {{myName}} is inside {{$parent.myName}} which is in {{$parent.$parent.myName }}<br/> "
}});
Run Code Online (Sandbox Code Playgroud)
相应的JSFiddle可在https://jsbin.com/nozuri/edit?html,js,output中找到
我得到的输出是
India
** Tamilnadu is inside India
**** Chennai is …Run Code Online (Sandbox Code Playgroud) 我有一个 JSON 数据,我想按字段分组,然后按计数排序。
var data = [{"Name":"Ravi","Country":"India"},
{"Name":"Alex","Country":"USA"},
{"Name":"Stew","Country":"UK"},
{"Name":"Mary","Country":"India"},
{"Name":"Raju","Country":"India"},
{"Name":"Bill","Country":"UK"},
{"Name":"Elisa","Country":"UK"},
{"Name":"Sharma","Country":"India"}];
Run Code Online (Sandbox Code Playgroud)
我的 d3.js 查询如下
var countryCount = d3.nest()
.key(function(d) { return d.Country; })
.rollup(function(a){return a.length;})
.entries(data);
console.log(JSON.stringify(countryCount));
Run Code Online (Sandbox Code Playgroud)
我的输出是
[{"key":"India","values":4},{"key":"USA","values":1},{"key":"UK","values":3}]
Run Code Online (Sandbox Code Playgroud)
我想要的输出是(按汇总值排序)
[{"key":"India","values":4},{"key":"UK","values":3},{"key":"USA","values":1}]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我知道以下浏览器默认排序方法也提供了所需的输出。但只是想弄清楚 d3.js 是否提供任何内置方法来实现这一点。
console.log(JSON.stringify(countryCount.sort(function (a, b){
if (a.values > b.values) {return -1;}
else if (a.values < b.values) { return 1;}
else return 0;
})));
Run Code Online (Sandbox Code Playgroud)