AngularJS 中范围原型 / 原型继承的细微差别是什么?

API 参考范围” 页面显示

范围可以从父范围继承。

开发人员指南范围页面显示

范围(典型地)从其父范围继承属性。

那么,子作用域是否总是从原型上继承自其父作用域?有例外吗?当它继承时,是否总是正常的 JavaScript 原型继承?

答案

<input type="text" ng-model="someObj.prop1">

<!--rather than
<input type="text" ng-model="prop1">`
-->
childScope.aString === 'parent string'
childScope.anArray[1] === 20
childScope.anObject.property1 === 'parent prop1'
childScope.aFunction() === 'parent output'
childScope.aString = 'child string'
childScope.anArray[1] = '22'
childScope.anObject.property1 = 'child prop1'
childScope.anArray = [100, 555]
childScope.anObject = { name: 'Mark', country: 'USA' }
delete childScope.anArray
childScope.anArray[1] === 22  // true
$scope.myPrimitive = 50;
$scope.myObject    = {aNumber: 11};
<script type="text/ng-template" id="/tpl1.html">
<input ng-model="myPrimitive">
</script>
<div ng-include src="'/tpl1.html'"></div>

<script type="text/ng-template" id="/tpl2.html">
<input ng-model="myObject.aNumber">
</script>
<div ng-include src="'/tpl2.html'"></div>
<input ng-model="$parent.myPrimitive">
// in the parent scope
$scope.setMyPrimitive = function(value) {
     $scope.myPrimitive = value;
}
$scope.myArrayOfPrimitives = [ 11, 22 ];
$scope.myArrayOfObjects    = [{num: 101}, {num: 202}]
<ul><li ng-repeat="num in myArrayOfPrimitives">
       <input ng-model="num">
    </li>
<ul>
<ul><li ng-repeat="obj in myArrayOfObjects">
       <input ng-model="obj.num">
    </li>
<ul>
childScope = scope.$new();  // child scope prototypically inherits from parent scope
...
childScope[valueIdent] = value;  // creates a new childScope property
myObject.prop = '123';
myObject.myThing.prop = '123';
function Parent(){

}
Parent.prototype = {
 primitive : 1,
 object : {
    one : 1
   }
}
function Child(){

}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.prototype.changeProps = function(){
    this.primitive = 2;
    this.object.one = 2;
};
var dad = new Parent();
var son = new Child();
son.changeProps();
console.log(dad.primitive); /* 1 */
console.log(son.primitive); /* 2 */
console.log(dad.object.one); /* 2 */
console.log(son.object.one); /* 2 */