问题是:那时angular 尚未更新其绑定 。
您不应该这样访问变量,请尝试使用angular js绑定机制将其绑定到 视图 (例如,使用$ watch)。绑定到父作用域变量意味着您是
被动的 ,只需侦听更改并更新 其他变量 或 视图即可 。这就是我们应该使用角度的方式。
如果您仍然需要访问它。您可以使用$
超时尝试解决方法
$scope.setDefaults = function() { $timeout(function () { alert(JSON.stringify($scope.appSubs)); //Coming as undefined },0); };演示
最好使用$ watch
angular.module('ctrl', []).controller('TempCtrl', function ($scope, $location, $rootScope) { $scope.appSubscriptions = "Subscriptions"; $scope.appObj = "Objs"; $scope.showAppEditWindow = function () { //Binding the directive isolate scope objects with parent scope objects $scope.asAppObj = $scope.appObj; $scope.asAppSubs = $scope.appSubscriptions; }; }); angular.module('ctrl').directive('tempDirective', function () { return { restrict: 'E', replace: true, scope: { appObj: '=asAppObj', appSubs: '=asAppSubs' }, link: function (scope, element, attrs) { }, controller: function ($scope, $timeout) { $scope.$watch("appSubs",function(newValue,OldValue,scope){ if (newValue){ alert(JSON.stringify(newValue));} }); }, template: "<div>{{appSubs}}</div>" }; });演示
通过使用$ watch,在这种情况下,您无需广播事件。



