首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >变化时的角度-无变化

变化时的角度-无变化
EN

Stack Overflow用户
提问于 2016-06-17 19:27:21
回答 1查看 54关注 0票数 0

我正在构建一个带有组件的站点导航,它是这样的:

代码语言:javascript
复制
<site-wrapper>
  <site-header>
    <site-menu />
  </site-header>
  <site-section />
  <site-section />
</site-wrapper>

现在,我的site-wrapper是这样的:

代码语言:javascript
复制
class siteWrapperController {
  $onInit() {
    this.sections = [];
  }

  addSection(section) {
    if (this.sections.indexOf(section) === -1) {
      this.sections.push(section);
    }
  }
}

app.component('siteWrapper', {
  controller: siteWrapperController,
  template,
  transclude: true
});

还有我的site-section

代码语言:javascript
复制
class siteSectionController {
  $onInit() {
    const id = this.sectionId;
    const title = this.sectionTitle;
    this.o = { id, title };
    this.wrap.addSection(this.o);
  }
}

app.component('siteSection', {
  template,
  transclude: true,
  controller: siteSectionController,
  bindings: {
    sectionTitle: '@',
    sectionId: '@'
  },
  require: {
    wrap: '^siteWrapper'
  }
});

我在这里所做的基本上就是在sections数组中注册wrapper中的每个部分。现在我想把这个sections传递给site-menu

代码语言:javascript
复制
class siteMenuController {
  $onInit() {
    this.buildSections(this.wrap.sections);
  }

  buildSections(sections) {
    Array.prototype.forEach.call(sections, s => {
      console.log(s);
    });
  }
}

app.component('siteMenu', {
  controller: siteMenuController,
  bindings: {
    items: '<',
    className: '@'
  },
  template: template,
  transclude: true,
  require: {
    wrap: '^siteWrapper'
  }
});

不幸的是,它返回空。这意味着,这运行得太早了。我可以直接把setTimeout放在那里,然后就完事了,但我相信还有更好的方法。我希望省略$scope$broadcast/$emit,而只是让组件以某种方式相互通信;-)

我能做什么?

EN

回答 1

Stack Overflow用户

发布于 2016-06-17 19:50:38

您可以监视变量,该变量将在变量的值发生变化时执行

例如,如果您的变量为$scope.newVar

然后你可以把watcher放在上面

代码语言:javascript
复制
$scope.$watch('newVar',function(newval,oldval){
 //your code goes here
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37880420

复制
相关文章

相似问题

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