javascript - How to access to the controller scope from a directive inside ng-repeat? - Stack Overflow

I'm a new user of AngularJS and I'm getting stuck when I have to access to a variable in a co

I'm a new user of AngularJS and I'm getting stuck when I have to access to a variable in a controller from a directive inside a ng-repeat. I think it has to be something related to scopes.

Here's my fiddle: /

The 'MyCtrl' scope has two properties: "items", an array, and "thing", just a string.

function MyCtrl($scope) {
    $scope.items = [{id: 'item1'}, {id: 'item2'}];    
    $scope.thing = "thing";
}

So, if I create a directive and I want it to read the array's content, this one works perfectly:

<div my-directive ng-repeat="item in items" item='item'></div>
app.directive('myDirective', function () {
    return {
        restrict: 'AE',
        scope: {
            item: '=item',
        },
        template: '<div>{{ item.id }}</div>'
    }
});

The HTML is refreshed properly.

But if I try to access to the "thing" variable from the directive, it is always read as "undefined".

app.directive('myDirective', function () {
    return {
        restrict: 'AE',
        scope: {
            item: '=item',
            thing: '='
        },
        template: '<div>{{ item.id }} - Here is not the {{thing}}</div>'
    }
});

I think the problem has to be related to the scope child created in the ng-repeat, or maybe the variable is not being bound correctly.

However, If I use $parent.thing in the template, the variable is read and it's evaluated properly, but I don't know if that $parent is the ng-repeat scope or 'MyCtrl' scope.

1) What am I doing wrong?

2) Why is it needed to put the "item" element read in the ng-repeat as an attribute of the HTML element? I first thought "items" was in the parent scope, so when the isolated scope of the directive is created, I would just have to do something like "items: '='".

I'm a new user of AngularJS and I'm getting stuck when I have to access to a variable in a controller from a directive inside a ng-repeat. I think it has to be something related to scopes.

Here's my fiddle: http://jsfiddle/Zzb58/1/

The 'MyCtrl' scope has two properties: "items", an array, and "thing", just a string.

function MyCtrl($scope) {
    $scope.items = [{id: 'item1'}, {id: 'item2'}];    
    $scope.thing = "thing";
}

So, if I create a directive and I want it to read the array's content, this one works perfectly:

<div my-directive ng-repeat="item in items" item='item'></div>
app.directive('myDirective', function () {
    return {
        restrict: 'AE',
        scope: {
            item: '=item',
        },
        template: '<div>{{ item.id }}</div>'
    }
});

The HTML is refreshed properly.

But if I try to access to the "thing" variable from the directive, it is always read as "undefined".

app.directive('myDirective', function () {
    return {
        restrict: 'AE',
        scope: {
            item: '=item',
            thing: '='
        },
        template: '<div>{{ item.id }} - Here is not the {{thing}}</div>'
    }
});

I think the problem has to be related to the scope child created in the ng-repeat, or maybe the variable is not being bound correctly.

However, If I use $parent.thing in the template, the variable is read and it's evaluated properly, but I don't know if that $parent is the ng-repeat scope or 'MyCtrl' scope.

1) What am I doing wrong?

2) Why is it needed to put the "item" element read in the ng-repeat as an attribute of the HTML element? I first thought "items" was in the parent scope, so when the isolated scope of the directive is created, I would just have to do something like "items: '='".

Share Improve this question asked Nov 9, 2013 at 23:52 JorgeJorge 2231 gold badge4 silver badges10 bronze badges 1
  • 1 If you use = then you have to pass the attribute like so thing="thing". – elclanrs Commented Nov 10, 2013 at 0:05
Add a ment  | 

1 Answer 1

Reset to default 4

By assigning an object to it, you create an isolate scope. If you set scope : true, you can use it like in your example, if not you have to pass it as an attribute, like you did with item.

http://jsfiddle/Zzb58/2/

For an explanation of different modes of scope, you can read the directives section on this page: https://github./angular/angular.js/wiki/Understanding-Scopes

Generally but especially when working with inherited scope values, I would also highly remend using an object with a property set, instead of a simple string-variable, as this might lead to a detachment throughout the scopes due to how prototypical inheritance works.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745003256a4605626.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信