我不知道这是要做到这一点的方式,但我的目标是:Angularjs:与孩子的指令集父指令范围值
- 我有一个父指令
- 父里面指令的块,我有一个孩子的指令,将获得来自用户
- 儿童指令将设置父指令的范围值,一些输入
- 我可以把它从那里
当然问题是父母和孩子的指令是兄弟姐妹。所以我不知道该怎么做。注 - 我不想在
小提琴组数据:http://jsfiddle.net/rrosen326/CZWS4/
HTML:
<div ng-controller="parentController">
<parent-dir dir-data="display this data">
<child-dir></child-dir>
</parent-dir>
</div>
的Javascript
var testapp = angular.module('testapp', []);
testapp.controller('parentController', ['$scope', '$window', function ($scope, $window) {
console.log('parentController scope id = ', $scope.$id);
$scope.ctrl_data = "irrelevant ctrl data";
}]);
testapp.directive('parentDir', function factory() {
return {
restrict: 'ECA',
scope: {
ctrl_data: '@'
},
template: '<div><b>parentDir scope.dirData:</b> {{dirData}} <div class="offset1" ng-transclude></div> </div>',
replace: false,
transclude: true,
link: function (scope, element, attrs) {
scope.dirData = attrs.dirData;
console.log("parent_dir scope: ", scope.$id);
}
};
});
testapp.directive('childDir', function factory() {
return {
restrict: 'ECA',
template: '<h4>Begin child directive</h4><input type="text" ng-model="dirData" /></br><div><b>childDir scope.dirData:</b> {{dirData}}</div>',
replace: false,
transclude: false,
link: function (scope, element, attrs) {
console.log("child_dir scope: ", scope.$id);
scope.dirData = "No, THIS data!"; // default text
}
};
});
布赖恩视频和耶稣的plunker是完美的。我加了一个编辑小提琴 - 使用$ watch不断更新父指令的范围(否则它只发生在耶稣的掠夺者身上)。 http://jsfiddle.net/rrosen326/7Dq4e/ –