javascript - Trouble accessing scope attribute in directive -
i trying access values in $scope.photores in directive of right console.log(scope.photores) in directive shows empty object. output console:
object {filename: "db372ec33603208781eb6fbf9789c816a4ab27d2.jpg", filepath: "c:\wamp\www\skittlize\photo_assets\uploads\db372ec33603208781eb6fbf9789c816a4ab27d2.jpg"} skittlesctrl.js:17 object {} skittlesctrl.js:37 'use strict'; angular.module('skittlesapp') .controller('skittlesctrl', function ($scope, $location){ $scope.photores = {}; $scope.dropzoneconfig = { 'options': { // passed dropzone constructor 'url': 'photo_assets/save2.php' }, 'eventhandlers': { 'sending': function (file, xhr, formdata) { }, 'success': function (file, response) { $scope.$apply(function () { $scope.photores = json.parse(file.xhr.getresponseheader("photoinfo")); $location.path('/uploader') console.log($scope.photores); }); } } }; }) .directive('dropzone', function () { return function (scope, element, attrs) { var config, dropzone; config = scope[attrs.dropzone]; dropzone = new dropzone(element[0], config.options); _.each(config.eventhandlers, function (handler, event) { dropzone.on(event, handler); }); }; }) .directive('imgswitch', ['photoserv', function (photoserv) { function link(scope, element, attrs) { scope.$watch('photores', function (newval) { if (newval) { console.log(scope.photores); var cropper = new crop(); cropper.init('.selfie-container'); cropper.loadimg('/uploads/'+scope.photores.filename); $('.cropbutton').on('click', function () { photoserv.skittlize('/uploads/'+scope.photores.filename); }); } }); }; return { link: link } }]);
is happening because change in parent scope isn't registering?
i doubt scope[attrs.dropzone]
valid. don't see anywhere you're assigning scope
within directive.
there 2 different ways solve this. 1 pass attribute (which may doing, don't know html looks like).
<div dropzone photores="{{photores}}"> </div>
then can call attrs.photores
value.
the other option assign scope within directive. you'd use @
, =
, or &
@
acceptable you. if remember correctly, @
takes value attribute. you'd still need pass in $scope
attribute.
=
on other hand bind $scope
object, , create 2-way data binding. don't need here.
.directive('dropzone', function() { return { restrict: "a", scope: { photores: "@", }, link: function(scope, element, attrs ) { console.log(attrs.photores); console.log(scope.photores); }; })
Comments
Post a Comment