我的模板加载后如何广播事件?

时间:2015-03-03 10:20:28

标签: angularjs angular-ui-router

编辑:似乎我搞砸了,指令是在模板加载后执行的,当我有更多时间时,我会把它排除在外并编辑我的问题

我正在使用ui-router进行模板/视图的条件加载,然后我想应用一个指令来修改这些模板的HTML。我想我会在加载模板时广播一个事件并将指令置于此事件上。但无法让它发挥作用。

这是我加载模板的方式:

FlatView.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise("");

    $stateProvider
      .state('flatView', {
          url:"",

          views: {
              'main': {
                  controller:"flatViewElementList",
                  templateUrl:Routing.generate('_NRtworks_FlatView_elementList') 
              },
              'discrimSelector': {
                  controller:"flatViewElementList",
                  templateProvider:['BDParameters','$http','$rootScope',function(BDParameters,$http,$rootScope){

                    return BDParameters.then(function (result) {
                        //console.log(result);
                        if(result["selectorList"] === 0)
                        {
                            return "";
                        }
                        else
                        {                            
                            return $http.get(Routing.generate('_NRtworks_FlatView_DiscrimSelector')).then(function(result)
                            {
                                //console.log(result.data);
                                $rootScope.$broadcast("NRtworks_DiscrimSelect");
                                return result.data;
                            });                           
                        }
                    });

                    //return Routing.generate('_NRtworks_FlatView_DiscrimSelector');

                  }],
                    resolve:
                    {
                        test:function(BDParameters,$rootScope){
                            $rootScope.$broadcast("NRtworks_DiscrimSelect");
                          }
                    }
              }
          }
      });    
});

这是指令:

FlatView.directive('NRtworks_discrimSelect',function($rootScope){
    var linkFunction = function(scope, element, attributes)
    {       
        $scope.$on("NRtworks_DiscrimSelectLoaded", function(){
                    transformDiscrimSelect(element);
        });

        function transformDiscrimSelect(element)
        {
            console.log("occured");
            element.on('change',function(event){
                //do stuff
            });
        };

    };
    return{
        restrict: 'AEC',
        link:linkFunction
    };}
);

首先,事件系统是否正确处理此问题?如果是,那么我做错了什么?如果不是,那么,我愿意接受建议;)

0 个答案:

没有答案