Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

angularjs - Isolate scope variable is undefined

For some reason I am not able to access my isolate scope variable "index" in my link function. I have tried both @ and = to see if it made any difference and this had no effect, I also tried using a word other than index (in case it was already in use by angular or something) with the same result. Error msg: ReferenceError: index is not defined.

directive:

var jobernizeDirectives = angular.module('jobernizeDirectives', []);

jobernizeDirectives.directive('viewLargeContent', function() {
    return {
        scope: {
            index: '=index' 
        },
        require: 'ngModel',
        link: function (scope, el, attrs, ngModel) {

            console.log(index);

            function openEditor() {
                console.log(index);

                var main_container = angular.element('.main_container');
                var view_frame = angular.element('.view-frame');

                console.log(ngModel);

                main_container.append('<div class="dimmer"></div>');
                main_container.append(

                        '<div class="large-content' + index + '">'
                    +   '<textarea>' + ngModel.$viewValue + '</textarea>' 
                    +   '<button>Submit Changes</button>'
                    +   '</div>'

                );  

                var dimmer = angular.element('.dimmer');
                var content_container = angular.element('.large-content' + index);      

                // content_container.css({ marginTop: (content_container.height()/-2), width: view_frame.width(), marginLeft: (view_frame.width()/-2) })
                content_container.css({ height: (0.8*main_container.height()), width: view_frame.width(), marginLeft: (view_frame.width()/-2)  })

                content_container.find('button').on('click', function() {
                    var new_content = content_container.find('textarea').get(0).value;

                    ngModel.$setViewValue(new_content);

                    content_container.hide();
                    dimmer.hide();
                });
            }

            // el.on('click', openEditor);
            el.on('click', openEditor);

        }
    }
});

html:

<div data-ng-controller="resumesController">

    <div class="row">
        <div class="small-12 columns">
            <h2>Your Resumes</h2>
        </div>
    </div>

    <div class="row">
        <div class="small-12 columns">
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Date Added</th>
                        <th>View/Edit Resume</th>
                    </tr>
                </thead>
                <tbody>
                    <tr data-ng-repeat="resume in resumes">
                        <td>{{ resume.name }}</td>
                        <td>{{ resume.category }}</td>
                        <td>{{ resume.date_added }}</td>
                        <td><button index="le" view-large-content data-ng-model="resume.content">View Resume</button></td>
                    </tr>
                </tbody>
            <table>
        </div>
    </div>

</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Index is an attribute of you scope object, so you access it like this:

scope.index

Both '@' or '=' will work, the difference is that '@' will interpolate the value and update the isolated scope when it changes and '=' means Angular should keep the attribute and the isolated scope in sync. If the variables have the same name you can use the syntactic sugar

scope: {
  index: '@' 
},

Here is a JSBin with your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...