如果我的“父”页面中有一个html元素,如::
<div ng-include"'Go-To-Child-Page.html'" />任何我的子/包含页面都是这样的:
<some-directive two-way-binding="$parent.SomeParentScope"></some-directive>,为什么这对我的指令无效?或者更好的是,我如何使它工作呢?
app.directive ('someDirective', function(){
return {
retrict: 'E',
replace: true,
scope: {
myBinding : "=twoWayBinding", <- this is what is not working
},
template: '<select ng-model="myBinding" ng-options="myType.Description for myType in myTypes"></select>'
};
}编辑更新:
我为什么要发这个问题?
在完成一个非常长的表单后,我立即注意到,我有相当多的类似的控件,我的编码器说我应该提取出来。其中之一是select控件。此控件涉及两种场景:
(1),在填充select控件之前,用户必须选择一个过滤器;以及
(2),其中代码预定义了select控件.的过滤器。
这两种情况的解决方案如下所示。我希望这对每个人都有帮助,因为我真的很喜欢使用角和它提供的创建“Html-魔术”的指令功能是惊人的。
发布于 2013-04-26 14:23:09
场景1:让用户筛选:
Filter:
<input type="radio" ng-model="choice.type" value="Rear"> Rear
<input type="radio" ng-model="choice.type" value="Front"> Front
<br>
Select:
<name-value-select selected-item="selected.item" choice="choice.type" options="axleTypes"></name-value-select>场景2:代码中的预筛选器:
<name-value-select preselected-filter="Front" selected-item="selected.item" options="axleTypes"></name-value-select>这两种场景的指令:
.directive('nameValueSelect', function () {
return {
replace: true,
restrict: "E",
scope: {
selectedItem: "=",
choice: "=",
options: "=",
preselectedFilter: "@"
},
controller: function ($scope) {
$scope.$watch('choice', function (selectedType) {
$scope.selectedItem = ''; // clear selection
$scope.filterTypes = [];
angular.forEach($scope.options, function (type) {
if ($scope.preselectedFilter !== undefined) {
selectedType = $scope.preselectedFilter;
}
if (type.Type === selectedType) {
this.push(type);
}
}, $scope.filterTypes);
$scope.filterTypes.sort(function (a, b) {
return a.Description.localeCompare(b.Description);
});
});
},
template: '<select ng-model="selectedItem" ng-options="o.Description for o in filterTypes"><option value="" selected="selected">Please Select </option></select>'
};
});和俗话说的强制性柱塞:
http://plnkr.co/edit/tnXgPKADfr5Okvj8oV2S
发布于 2013-04-24 15:16:07
你似乎做了很多不必要的事情,但那可能是因为我误解了你的目标。
我已经修正了你的观点:http://plnkr.co/edit/FqpzC5p1Ogm4attYiArV?p=preview
必要的基本改革似乎是:
ngOptions替换为ngRepeat和过滤器您的指令实际上不需要有控制器(通常大多数指令应该使用链接器函数)。为了使它更简单,我去掉了一些片段,但是您仍然可以按原来的方式连接$scope.filterTypes (从$scope.myTypes中提取可用类型),它仍然可以正常工作。
更新
由于您没有详细说明所有的需求,所以我可能遗漏了一些需求,但是这个实现是我收集到的您要寻找的:
http://plnkr.co/edit/pHgJ84Z35q5jxCpq3FHC?p=preview
它有动态过滤,不需要不必要地使用控制器,而是双向绑定。唯一的问题是它引用了"Description“字段(和原来的一样)。如果您愿意,可以在HTML中进行配置。
https://stackoverflow.com/questions/16194351
复制相似问题