te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - In AngularJS, how to make an isolated scope inherit from ng-repeat's scope - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In AngularJS, how to make an isolated scope inherit from ng-repeat's scope - Stack Overflow

programmeradmin4浏览0评论

I'm trying to create a custom ponent that receives arguments in a ng-repeat loop. So for example, say I have a ponent named "myp" that receives a custom argument "name" in a ng-repeat:

<myp name="{obj.name}" ng-repeat="obj in list" />

And in my directive the isolated scope is defined like this:

scope:{name:"@"}

That won't work because ng-repeat creates an isolated scope for each element it iterates. So I ended up having two levels of scopes.

How do I get around this issue? Am I doing something wrong?

Thanks.

I'm trying to create a custom ponent that receives arguments in a ng-repeat loop. So for example, say I have a ponent named "myp" that receives a custom argument "name" in a ng-repeat:

<myp name="{obj.name}" ng-repeat="obj in list" />

And in my directive the isolated scope is defined like this:

scope:{name:"@"}

That won't work because ng-repeat creates an isolated scope for each element it iterates. So I ended up having two levels of scopes.

How do I get around this issue? Am I doing something wrong?

Thanks.

Share Improve this question edited Oct 3, 2013 at 19:12 yohairosen asked Nov 19, 2012 at 8:09 yohairosenyohairosen 1,6851 gold badge18 silver badges26 bronze badges 2
  • Already answered here: stackoverflow./questions/13294507/… – Tiago Roldão Commented Nov 19, 2012 at 10:28
  • We need more code. It's hard to tell what you're trying to do here. – Ben Lesh Commented Nov 19, 2012 at 14:27
Add a ment  | 

1 Answer 1

Reset to default 16

As I stated in my ment of your original question, this has already been answered. Anyway, here it is, summed up:

In your template, state the model you want to have inherited, without {{}} (as using brackets results in the value being passed, and not the reference to the model itself):

<myp name="obj.name" ng-repeat="obj in list" />

And in your directive, establish a 2-way binding, like so:

scope:{name:"="}

EDIT:

I realize now (after your ment) that while this solves your problem, it doesn't fully answer the question. Here goes:

When you create a directive you have the choice of creating a scope that inherits from its parent (controller, typically, though not necessarily) ou an "isolated" scope, by specifying scope: true or scope: {...}, respectively.

So, by creating an unisolated scope, all the parent's models are available (you can access scope.obj - created via ng-repeat - but also scope.list). This is convenient, but also dangerous, of course (and doesn't really create reusable code).

If you create an isolated scope, you can specify the scope's models using '@', '=' or '&'.

'@' and '&' both produce a isolated, unbinded value, (that, if you change, changes only on the isolated scope - in your case, the object in the original list suffers no change at all), the only difference being that '@' reads a string value, and '&' reads an expression. THIS IS IMPORTANT: the reason why I believe your code didn't work was (only) because you passed name="{obj.name}" and not name="{{obj.name}}", for with '@' the string value is read, and that string value can be the name of obj, but you must include it in {{}}!

If you use '=', you are declaring that you want that variable to be binded with the specified outside variable. So, if (in a fit of crazy, crazy rage!) you want to have 2 models in your directive that start up with the same value, but on is binded (i.e. changes are propagated to the outside scope), you could do something like this:

<myp binded-name="obj.name" unbinded-name="{{obj.name}}" ng-repeat="obj in list" />

and in your directive:

scope:{
  bindedName: "=",
  unbindedName: "@"
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论