首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子作用域不更改父作用域

子作用域不更改父作用域
EN

Stack Overflow用户
提问于 2014-09-18 23:07:25
回答 1查看 226关注 0票数 0

我有两个指令,正在尝试根据选择框过滤表。我正在使用ui.bootstrap创建一个手风琴来容纳表单。由于某些原因,我的窗体在单击时不会更改父范围。我认为"=“应该改变父作用域,但它似乎没有做任何事情IE:我想选择DATE,Line Item,Creative,然后只看到表中的那些字段。

代码语言:javascript
复制
//table.js

var TableCtrl = function($scope,$rootScope){
  $scope.headings = ['Date', 'Creative', 'Line item',
                       'Interactive impressions','Interaction time',
                       'Total interactions','Ad server clicks', 'Average interaction time'];


  $scope.selectedHeadings = ['Date', 'Creative', 'Line item',
                               'Interactive impressions','Interaction time',
                               'Total interactions','Ad server clicks', 'Average interaction time'];
app.directive('campaignStatsTable',function(){
  return {
    $scope: true,
    restrict: 'EA',
    replace: 'true',
    templateUrl: './views/templates/tables/table.html'
  };
});

app.directive('tableFilter',function(){
  return {
    $scope: "=",
    restrict: 'EA',
    replace: 'true',
    templateUrl: './views/templates/tables/table-filter.html'
  };
});

app.controller('TableCtrl', ['$scope' ,'$rootScope', TableCtrl]);

我的html主页

代码语言:javascript
复制
<accordion>
    <accordion-group heading="Click to Edit Filtering Options">
        <table-filter></table-filter>
    </accordion-group>
  </accordion>

我的表单模板

代码语言:javascript
复制
<form id="table-filters" role="search">
  <div class="form-group">
    <select id='headerSelection' multiple ng-multiple=true ng-show='headings' ng-model="selectedHeadings" ng-options="heading for heading in headings"></select>
  </div>
  <div class="form-group">
    <input type="text" class="form-control" placeholder="Line Item ID" ng-model="LineItemID">
  </div>
</form>

我的表格模板

代码语言:javascript
复制
<table class="table table-striped">
  <th ng-repeat="header in selectedHeadings">
    {{header}}
  </th>
  <tr ng-repeat="item in tableRecords | filter:Date | filter:line_item " item='item'>
    <td ng-repeat="header in selectedHeadings" id='{{header}}' ng-href='#'>
      <div ng-if="header == 'Date'">
        {{item[header] | date:'EEE, MMM, dd'}}
        <i class="btn fa fa-bar-chart" ng-click="refreshGraph(data,header, item[header])"></i>
      </div>
      <div ng-if="header != 'Date'">
        {{item[header]}}
        <i class='btn fa fa-bar-chart' tooltip='Click to sort the chart using this value' ng-if="['Creative', 'Line item'].indexOf(header) > -1" ng-click="refreshGraph(data,header, item[header])" ></i>
      </div>
    </td>
  </tr>
</table>
EN

回答 1

Stack Overflow用户

发布于 2014-09-18 23:25:42

我想通了。我已经到处都有$parent.selectedHeadings了,但它似乎不能控制我的行为。在将$scope.selectedHeadings转换为对象后,它开始按预期工作。即:

代码语言:javascript
复制
$scope.selectedHeadings = {names: ['Date', 'Creative', 'Line item',
                                   'Interactive impressions','Interaction time',
                                   'Total interactions','Ad server clicks', 'Average interaction time']

并将HTML中对selectedHeadings的所有引用更改为selectedHeadings.names。

这里有一个指向StackOverflow答案的链接,它很有帮助。

https://stackoverflow.com/a/21270386/2007602

主要的部分是

不正确:

代码语言:javascript
复制
ng-model="selectedIcon"

正确:

代码语言:javascript
复制
ng-model="obj.selectedIcon"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25916314

复制
相关文章

相似问题

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