权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - AngularJS - problems with $onChanges - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS - problems with $onChanges - Stack Overflow

programmeradmin8浏览0评论

I have an object defined in the parent ponent and passed in one-way binding to first-child and second-child ponents. First-child ponent make changes in the object. I cannot understand why in the second-child ponent $onChanges doesn't fire even if the object have changed something.

I have a project build as in this snippet (link to JSFiddle for pleteness)

angular.module('test', [])
ponent('parent', {
  template: "<first-child bind='vm.obj'></first-child>\
           <second-child bind='vm.obj'></second-child>\
    ",
  controller: function() {
    this.obj = {
      value: 0
    };
    this.$onInit = function() {}
  },
  controllerAs: 'vm',
  bindings: {}
})
ponent('firstChild', {
  template: "<button ng-click='vm.plus()'>+</button>\
           <button ng-click='vm.minus()'>-</button>\
          ",
  controller: function() {
    this.plus = function() {
      this.bind.value++;
    }

    this.minus = function() {
      this.bind.value--;
    }

    this.$onInit = function() {}

    this.$onChanges = function(obj) {
      console.log('first-child changed one-way bindings', obj)
    }
  },
  controllerAs: 'vm',
  bindings: {
    bind: '<'
  }
})
ponent('secondChild', {
  template: "<p>{{vm.bind.value}}</p>",
  controller: function() {
    this.$onInit = function() {}
    this.$onChanges = function(obj) {
      console.log('second-child changed one-way bindings', obj)
    }
  },
  controllerAs: 'vm',
  bindings: {
    bind: '<'
  }
})
<script src=".js/1.6.1/angular.min.js"></script>
<script src=".1.1/jquery.min.js"></script>

<body ng-app="test">
  <parent></parent>
</body>

I have an object defined in the parent ponent and passed in one-way binding to first-child and second-child ponents. First-child ponent make changes in the object. I cannot understand why in the second-child ponent $onChanges doesn't fire even if the object have changed something.

I have a project build as in this snippet (link to JSFiddle for pleteness)

angular.module('test', [])
.ponent('parent', {
  template: "<first-child bind='vm.obj'></first-child>\
           <second-child bind='vm.obj'></second-child>\
    ",
  controller: function() {
    this.obj = {
      value: 0
    };
    this.$onInit = function() {}
  },
  controllerAs: 'vm',
  bindings: {}
})
.ponent('firstChild', {
  template: "<button ng-click='vm.plus()'>+</button>\
           <button ng-click='vm.minus()'>-</button>\
          ",
  controller: function() {
    this.plus = function() {
      this.bind.value++;
    }

    this.minus = function() {
      this.bind.value--;
    }

    this.$onInit = function() {}

    this.$onChanges = function(obj) {
      console.log('first-child changed one-way bindings', obj)
    }
  },
  controllerAs: 'vm',
  bindings: {
    bind: '<'
  }
})
.ponent('secondChild', {
  template: "<p>{{vm.bind.value}}</p>",
  controller: function() {
    this.$onInit = function() {}
    this.$onChanges = function(obj) {
      console.log('second-child changed one-way bindings', obj)
    }
  },
  controllerAs: 'vm',
  bindings: {
    bind: '<'
  }
})
<script src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body ng-app="test">
  <parent></parent>
</body>

So my question is, is there a way to fire $onChanges in this situation?

$onChanges doesn't fire even if I define an array in parent, pass to first-child and second-child in one-way binding and in first-child I push something. But in that case the object has changed, so i still don't undertand why.

Share Improve this question edited Apr 29, 2023 at 20:05 ggorlen 57.4k8 gold badges111 silver badges154 bronze badges asked Apr 7, 2017 at 18:31 Matteo MeilMatteo Meil 1,35311 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You are misunderstanding the usage of one-way binding a little.

One-way binding will trigger $onChanges if the object changes, but not if a property of the object changes.

You must bind directly to the value in secondChild, not the full object.

Here is a solution to your problem:

angular.module('test', [])
.ponent('parent', {
  template: "   <first-child bind='vm.obj'></first-child>\
                            <second-child value='vm.obj.value'></second-child>\
                        ",
  controller: function(){
    this.obj = {value: 0};
    this.$onInit = function(){}
    },
  controllerAs: 'vm',
  bindings: {}
})
.ponent('firstChild', {
    template: " <button ng-click='vm.plus()'>+</button>\
                            <button ng-click='vm.minus()'>-</button>\
                        ",
  controller: function(){
    
    this.plus = function(){
        this.bind.value++;
    }
    
    this.minus = function(){
        this.bind.value--;
    }
        
    this.$onInit = function(){}
        
    this.$onChanges = function(obj){
        console.log('first-child changed one-way bindings', obj)
        }
    },
  controllerAs: 'vm',
  bindings: {
    bind: '<'
  }
})
.ponent('secondChild', {
  template: "<p>{{vm.value}}</p>",
  controller: function(){
        this.$onInit = function(){}
        this.$onChanges = function(obj){
            console.log('second-child changed one-way bindings', obj)
        }
  },
  controllerAs: 'vm',
  bindings: {
    value: '<'
  }
})

Quick advice: I think you should work with two-way binding with firstChild, directly on the value, and then one-way in secondChild directly with value too.

Your fiddle with correction.

发布评论

评论列表(0)

  1. 暂无评论