2016-04-29 57 views
13

我正在尝试在指令中的“输入”事件后执行某些操作。当模板中加载的事件不会开火。AngularJS指令中的动画,事件未触发

这里是应用程序声明

angular 
    .module('MyApp', [ 
    'ngAnimate', 
    'ngCookies', 
    'ngResource', 
    'ngRoute', 
    'ngSanitize', 
    'ngTouch' 
    ]) 
    .config(function ($routeProvider) { 
    $routeProvider 
     .when('/', { 
     templateUrl: 'views/home.html', 
     controller: 'HomeCtrl' 
     }) 
     .when('/in-the-community', { 
     templateUrl: 'views/in-the-community.html', 
     controller: 'CommunityCtrl' 
     }) 
     .otherwise({ 
     redirectTo: '/' 
     }); 
    }); 

起初我使用的路由提供给我一个模板页面。然后我试图在这些模板中使用一个指令来提供另一个视图。这是通过使用我的模板页面

<div class="flex"> 
    <div class="fc fci image-wrapper" id="home-banner"> 
    </div> 

    <div class="fc fcc"> 
     <section show-and-hide-content="{{ sub_content }}"> 
     </section> 
    </div> 
</div> 

这载荷以下指令

angular.module('rubisApp') 
    .directive('showAndHideContent', function ($animate) { 
    return { 
     templateUrl: 'views/community-sub.html', 
     controller: 'CommunitySubCtrl', 
     link: function(scope, element, attributes) { 
     $animate.on('enter', element, 
      function callback(element, phase) { 
      console.log('attributes.showAndHideContent'); 
      } 
     ); 
     } 
    }; 
    }); 

控制台日志未运行,而我只能推测如下因为它不点火$animate.on事件。

角度是否将ng-enter类应用于指令中的templateUrl

我很新的角度,所以如果这是这样做的错误方式,替代品也真的有帮助。

+0

从来没有听说过“进入”事件,并且无法通过谷歌找到关于它的消息 - 您是否指“按Enter键”时按下了? – messerbill

+1

@messerbill,'enter'是一个angularjs结构事件,@ChrisTownsend什么是'container'?你有没有把它定义在某个你没有展示的地方?还是你的意思是使用'element'? –

+0

不,所以对于动画提供者,当使用ng-view时,它会在不同的状态下添加类。我试图做一个指令,做一些回调 –

回答

3

$ animate依赖关系未被拉入指令。

angular.module('MyApp') 
    .directive('showAndHideContent',['$animate', function ($animate) { 
    return { 
     templateUrl: 'views/community-sub.html', 
     controller: 'CommunitySubCtrl', 
     link: function(scope, element, attributes) { 
     $animate.on('enter', element, 
      function callback(element, phase) { 
      console.log('attributes.showAndHideContent'); 
      } 

     ); 

     } 

    }; 
    }]); 
0

我看到你正在使用控制器。你不能在那里创建活动吗? 我检查了我的代码,并在控制器上有此代码段,以便在路由更改时销毁间隔,注意我使用$和'$ destroy',使用$。可以这样吗?

$scope.$on('$destroy', function() { 
        $interval.cancel($scope.interval); 
       });