Accessing scope in a link function within a directive is "undefined" following minification for a number of examples (using Closure Compiler) but works fine pre-minification.
For example, code below from the Angular tutorial with some alterations. After minification $scope.format is not picked up by the directive at any point. The directive shows the default format (Jul 15, 2015) without any errors. Prior to minification, the directive displays format as defined in $scope.format (7/15/2015 12:09:04 AM).
app.js
.controller('Controller', ['$scope', function ($scope) {
$scope.format = 'M/d/yy h:mm:ss a';
}])
.directive('myCurrentTime', bd.myCurrentTime.Directive.factory)
myCurrentTime-directive.js
'use strict';
goog.provide('bd.myCurrentTime.Directive.factory');
/**
* Example directive factory.
*
* @return {Object}
* @ngInject
* @export
*/
bd.myCurrentTime.Directive.factory = function ($interval, dateFilter) {
function link(scope, element, attrs) {
var format,
timeoutId;
function updateTime() {
element.text(dateFilter(new Date(), format));
}
scope.$watch(attrs.myCurrentTime, function (value) {
format = value;
updateTime();
});
element.on('$destroy', function () {
$interval.cancel(timeoutId);
});
// start the UI update process; save the timeoutId for canceling
timeoutId = $interval(function () {
updateTime(); // update DOM
}, 1000);
}
return {
link: link
};
};
// Added by ng-annotate
bd.myCurrentTime.Directive.factory.$inject = ["$interval", "dateFilter"];
html file:
<div ng-controller="Controller">
Date format: <input ng-model="format"> <hr />
Current time is: <span my-current-time="format"></span>
</div>
$scope.formatto$scope.fbut delete it because it not used in function