如何在角度js中使用$ emit后重新加载数据

时间:2015-03-31 09:58:06

标签: javascript angularjs cordova

我对angularJs有一些疑问。在使用emit之后如何在范围内重新启动一些变量。示例代码:



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

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

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




3 个答案:

答案 0 :(得分:0)

您使用数据传递给接收参数$的回调:

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

答案 1 :(得分: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;
	
      $scope.count++;
    });
  }]);

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

答案 2 :(得分:0)

以下代码未给出正确的结果:

继承的范围值不会在$ emit函数中给出正确的结果,并且第一次发射函数将无法按预期在角度js中起作用。

Plz任何澄清它。