Skip to content

Instantly share code, notes, and snippets.

@crazylion
Last active December 22, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazylion/6399133 to your computer and use it in GitHub Desktop.
Save crazylion/6399133 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html ng-app="test">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body>
<div ng-controller="mainCtrl">
<dir-test></dir-test>
</div>
<script src="main.js"></script>
</body>
</html>
var proApp = angular.module("test",[]) ;
proApp.factory("shareData",function($rootScope){
return {
share: function(urlName){
console.log(urlName);
$rootScope.$broadcast("urlChange",urlName);
}
}
});
function mainCtrl($scope,$location,$shareData){
$scope.$watch(function(){return $location.path()},function(path){
console.log(path) ;
$shareData.share(path);
})
}
proApp.directive("dirTest",function(){
return {
restrict: "E",
template:'hello {{urlName}}',
controller: function($scope){
$scope.$on("urlChange",function(evt,urlName){
console.log("directive:",urlName) ;
$scope.urlName=urlName;
}) ;
}
}
});
mainCtrl.$inject=["$scope","$location","shareData"];
var proApp = angular.module("test",[]) ;
proApp.factory("shareData",function($rootScope,$timeout){
var app=function(urlName){
$timeout(function(){
console.log("inTimeout");
$rootScope.$broadcast("urlChange",urlName);
app=null;
app=function(urlName){
console.log("not in Timeout");
$rootScope.$broadcast("urlChange",urlName);
};
},100);
}
return {
share: function(urlName){
console.log(urlName);
app(urlName);
// $rootScope.$broadcast("urlChange",urlName);
}
}
});
function mainCtrl($scope,$location,$shareData){
$scope.$watch(function(){return $location.path()},function(path){
console.log(path) ;
$shareData.share(path);
})
}
proApp.directive("dirTest",function(){
return {
restrict: "E",
template: "heello",
controller: function($scope){
$scope.$on("urlChange",function(evt,urlName){
console.log("directive:",urlName) ;
$scope.urlName=urlName;
}) ;
}
}
});
mainCtrl.$inject=["$scope","$location","shareData"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment