在指令中,定义范围:{isoNetwork:'=network'},如果父范围没有'network'属性,它会自动创建它吗?以下图片是父范围:
这是代码示例(在 jsFiddle 中相同:http: //jsfiddle.net/yougen/mvYrJ/7/):
var app = angular.module('app',[])
app.controller('AppCtrl', function($scope){
$scope.leaveVoicemail = function(number, message, network){
console.log("controller scope: ", $scope);
window.alert("Number: " + number + " said: " + message + ", by network: " + network);
};
});
app.directive('phone', function(){
return {
restrict:'E',
scope:{
isolatedAttributeNumber:'@number',
isoNetwork:'=network',
makeCall:'&'
},
link:function(scope){
scope.networks = ["Verizon", "AT&T", "Sprint"];
scope.isoNetwork = scope.networks[1];
}
};
});
html:
<div ng-app="app" ng-controller="AppCtrl">
<phone number="555-1234" network="network" make-call="leaveVoicemail(number, message, network)">
<div>
Number:{{isolatedAttributeNumber}} Network:<select ng-model="isoNetwork" ng-options="net for net in networks"></select>
</div>
Your message:<input type="text" ng-model="voiceMessage"><br>
<button class="btn" ng-click="makeCall({number: isolatedAttributeNumber, message: voiceMessage, network: isoNetwork})">Call Home!</button>
</phone>
</div>