2015-03-31 65 views
0

我有一些關於angularJs的問題..如何重新初始化一些變量後,我使用發射範圍。示例代碼:

$scope.uiConfig = {title: "example"}; 
 

 
$scope.$emit('myCostumCalendar', 'Data to send'); 
 

 
$scope.$on('myCostumCalendar', function() { 
 
    $scope.uiConfig = {title: "new Title"}; 
 
});

回答

0

傳遞給$與您的數據接收的參數回調:

$scope.$on('myCostumCalendar', function (event, data) { 
    // data will store your sent data 
    $scope.uiConfig = {title: "new Title"}; 
}); 
0

var myApp = angular.module('myApp',[]); 
 

 
    myApp.controller('mainEventController', ['$scope', function($scope) { 
 
    $scope.count = 0; 
 
    }]); 
 
    myApp.controller('EventController', ['$scope', function($scope) { 
 
    debugger; 
 

 
    $scope.$on('MyEvent', function() { 
 
     debugger; 
 
\t 
 
     $scope.count++; 
 
    }); 
 
    }]);
<!DOCTYPE HTML> 
 
<html> 
 
\t <head> 
 
\t \t <title>Basic on controllers</title> 
 
\t \t <script src="//code.angularjs.org/snapshot/angular.min.js"></script> 
 
\t \t <script src="basiccontroller.js"></script> 
 
\t </head> 
 
\t <body ng-app="myApp"> 
 
\t 
 
\t \t <!-- emit and broadcast --> 
 
\t \t <p><b>emit and broadcast</b></p> 
 
\t \t <p>----------------------------</p> 
 
\t \t <div ng-controller="mainEventController"> 
 
\t \t Root scope <tt>MyEvent</tt> count: {{count}} 
 
\t \t <ul> 
 
\t \t \t <li ng-controller="EventController"> 
 
\t \t \t \t <button ng-click="$emit('MyEvent')">$emit('MyEvent')</button> 
 
\t \t \t \t <button ng-click="$broadcast('MyEvent')">$broadcast('MyEvent')</button> 
 
\t \t \t \t <br> 
 
\t \t \t \t Middle scope <tt>MyEvent</tt> count: {{count}} 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li ng-controller="EventController"> 
 
\t \t \t \t \t \t Leaf scope <tt>MyEvent</tt> count: {{count}} 
 
\t \t \t \t \t </li> 
 
\t \t \t \t </ul> 
 
\t \t \t </li> 
 
\t \t </ul> 
 
\t \t </div> 
 
\t \t 
 
\t </body> 
 
</html>

0

下面的代碼是不給予適當的r結果:

繼承的範圍值不會在$ emit函數中給出正確的結果,並且第一次發出函數將不會像預期的那樣在角度js中工作。

Plz任何澄清它。

+0

如果你的代碼有問題,只需要問一個新問題,而不是像這樣回答。 – Pengyy 2017-03-31 07:37:21

相關問題