作用域层级和继承
在上节的基础上,我们想显示出居住在给定国家的人口占世界人口的百分比。
为了满足此需求,在worldctrl管理的作用域中定义worldsPercentage函数,代码如下:
$scope.worldsPercentage=function(coutryPopulation){
return (coutryPopulation/$scope.populations);
}
之后,在ng-repeat指令创建的作用域中调用此函数:
<ul ng-controller="WorldCtrl">
<li ng-repeat="country in countries">
{{country.name}} has population of {{country.population}},
{{worldsPercentage(country.population)}}% of the world's population.
</li>
<hr>
World's population:{{population}} millions.
</ul>
AngularJS中的继承(inheritance)和JavaScript中的原型继承遵循同样的规则(延继承树向上查找属性,知道找到为止)