AngularJS : directives and scope

后端 未结 4 1606
无人及你
无人及你 2021-01-21 01:39

I have a simple question that I think has a pretty simple solution, but somehow I am missing it.

I\'m setting up a simple directive for my app. I\'m leaving the scope pr

4条回答
  •  深忆病人
    2021-01-21 01:45

    It looks like you are using angular $resource for your MachineServices, right? That creates an empty object or array for your results that you can bind to immediately that it later fills with results when the call completes asynchronously. When link is called by angular it will not be populated with data yet.

    First add logging to your query and getByCustomerId calls to make sure they are getting the values and setting the properties on the scope correctly. Add {{pages|json}} somewhere in your template to see the JSON of pages as it is changed. If you see that then you can watch that property in your directive if you need to do something special when it changes:

    MyDirectives.directive("mdPagination", function(){
        return {
            templateURL: "/templates/pagination.html",
            replace: true,
            restrict: 'A',
            scope: false, //default
            link: function(scope, element, attrs){ 
                console.log('in linking function');
                console.dir(scope);
                console.dir(element);
                console.dir(attrs);
                console.dir(scope.pages);
    
                // watch pages for changes
                scope.$watch('pages', function(value) {
                    console.log("pages now has this value:");
                    console.dir(scope.pages);
                });
            }
        }
    });
    

提交回复
热议问题