首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS -隔离作用域绑定到父作用域

AngularJS -隔离作用域绑定到父作用域
EN

Stack Overflow用户
提问于 2013-04-24 14:15:00
回答 2查看 3.5K关注 0票数 1

如果我的“父”页面中有一个html元素,如:

代码语言:javascript
复制
   <div ng-include"'Go-To-Child-Page.html'" />

任何我的子/包含页面都是这样的:

代码语言:javascript
复制
   <some-directive two-way-binding="$parent.SomeParentScope"></some-directive>

,为什么这对我的指令无效?或者更好的是,我如何使它工作呢?

代码语言:javascript
复制
 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-魔术”的指令功能是惊人的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-26 14:23:09

场景1:让用户筛选:

代码语言:javascript
复制
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:代码中的预筛选器:

代码语言:javascript
复制
<name-value-select preselected-filter="Front" selected-item="selected.item" options="axleTypes"></name-value-select>

这两种场景的指令:

代码语言:javascript
复制
.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

票数 0
EN

Stack Overflow用户

发布于 2013-04-24 15:16:07

你似乎做了很多不必要的事情,但那可能是因为我误解了你的目标。

我已经修正了你的观点:http://plnkr.co/edit/FqpzC5p1Ogm4attYiArV?p=preview

必要的基本改革似乎是:

  1. 将选定的过滤器(后/前)传递到您的指令中
  2. ngOptions替换为ngRepeat和过滤器

您的指令实际上不需要有控制器(通常大多数指令应该使用链接器函数)。为了使它更简单,我去掉了一些片段,但是您仍然可以按原来的方式连接$scope.filterTypes (从$scope.myTypes中提取可用类型),它仍然可以正常工作。

更新

由于您没有详细说明所有的需求,所以我可能遗漏了一些需求,但是这个实现是我收集到的您要寻找的:

http://plnkr.co/edit/pHgJ84Z35q5jxCpq3FHC?p=preview

它有动态过滤,不需要不必要地使用控制器,而是双向绑定。唯一的问题是它引用了"Description“字段(和原来的一样)。如果您愿意,可以在HTML中进行配置。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16194351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档