angularjs - Assignment to scope isn't working in directive -
i have directive below makes change in scope element sections.data
later assign
update data on ui. however, approach works in function change_subnav
not in switch case (added comments code). why happening? code change same. appreciate here. please let me know if need add more information.
plunker- https://embed.plnkr.co/wmojcq/
.directive('selectsubnav', function ($parse) { return { restrict: 'a', link: function (scope, element, attr) { var change_subnav = function (subnav) { if (scope.active_tab == 'user_exercises') { var sections = {}; sections[subnav] = scope[scope.active_tab][subnav]; // works scope.$apply(function () { $parse('sections.data').assign(scope.$parent, sections); }); } else { } }; $(element).on('click', function () { $(element).parent().children().removeclass('active'); $(element).addclass('active'); switch (attr.selectsubnav) { case 'all': // doesn't work scope.$apply(function () { $parse('sections.data').assign(scope.$parent, scope[scope.active_tab]); console.log(scope.sections.data); }); break; default: change_subnav(attr.selectsubnav); break; } }); } } })
update
.directive('selectsubnav', function ($parse) { return { restrict: 'a', link: function (scope, element, attr) { var currentscope = scope; var change_subnav = function (subnav) { if (scope.active_tab == 'user_exercises') { var sections = {}; sections[subnav] = scope[scope.active_tab][subnav]; } else { } return sections; }; $(element).on('click', function () { $(element).parent().children().removeclass('active'); $(element).addclass('active'); var sections; switch (attr.selectsubnav) { case 'all': // doesn't work sections = currentscope[currentscope.active_tab]; break; default: sections = change_subnav(attr.selectsubnav); break; } currentscope.$apply(function () { $parse('sections.data').assign(currentscope.$parent, sections); }); }); } } })
you have access scope in link function, instead of updating scope.sections.data this:
$parse('sections.data').assign(scope.$parent, sections);
you can this:
$parse('sections.data').assign(scope, sections);
or, better yet update directly, this:
scope.sections.data = sections;
Comments
Post a Comment