簡體   English   中英

從ngView訪問鏈接功能?

[英]Accessing link function from ngView?

對於Angular的初學者,我可能措辭不正確,但是我不知道該怎么形容。

這樣發生路線變更時

.when('/manage/users', {
    templateUrl: 'Angular/manage/users/overview.html',
    controller: 'usersCtrl'
    }
})

無論如何,是否有訪問加載模板的compilelink函數的權限,就像它是指令一樣?

我想在要加載的路線上在模板內添加一個加載動畫,但是我沒有看到一種訪問模板的方法,就好像它是一個指令(我通常會使用link

編輯:我正在嘗試做的例子

當上述路由被調用時

overview.html

<div ng-controller="usersCtrl" class="listPane">
 <div class="loader">Loading...</div> <!--Relevant Div I want to control-->
    <div ng-repeat="group in listGroups">
        <div class="tile-group max">
            <div class="tile-group-title"><a>{{group.title}}</a></div>
                <div listview items="group.items"></div>
           </div>
    </div>
</div>

我的控制器執行異步GET獲取用戶列表

app.controller('usersCtrl', ['$scope','companyService', function($scope, companyService){

$scope.listGroups = {title:'All Users',
                     items:[]};

$scope.listAsyncAnimate = true; //variable I would like to use to control showing or hiding loading div

$scope.$watch('listGroups', function(newVal, oldVal) {
    $scope.listGroups = newVal;

});
    companyService.getUsers(1).then(function(data)
    {
        $scope.listAsyncAnimate = false; //turn off loading div after list has been returned
        $scope.listGroups = [{
            title: 'All Users',
            items:  Utility.userlistToMetroList(data)
        }];
    });
}]);

在其他指令中,我還使用上述控件功能以及監視變量來控制link可見性,當我對模板進行路由更改時,我無權訪問該linkoverview.html ):

link: function (scope, element, attr) {

var loader = $(element).find('.loader');

scope.$watch('listAsyncAnimate', function(newVal, oldVal){
           if(newVal)
           {
               loader.spin('small');
               loader.removeClass('ng-hide');
           }
            else
           {
               loader.addClass('ng-hide');
           }
        });
 }

這是一個小矮人

只需創建一個指令:

app.directive('myDirective', function($route){
  return {  
    link: function(){
      var link = $route.current.link || angular.noop;
      link.apply(null,arguments);
    }
  }
})

並將其添加到ngView

<div ng-view my-directive></div>

現在在路線定義中,您具有鏈接功能:

.when('/manage/users', {
    templateUrl: 'Angular/manage/users/overview.html',
    controller: 'usersCtrl',
    link: function(scope,elm,attrs){
       // do your magic
    }
  }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM