多控制器作为一个指令的语法?

时间:2015-12-11 10:36:54

标签: angularjs angularjs-directive

我有下面的路由和控制器

.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/', {
        templateUrl : '/data/user.html',
        controller : 'UserController as usr'
    })
    .when('/add-schema', {
        templateUrl : '/data/group.html',
        controller : 'GroupController as grp'
   })

}])

/*Note That I dont want to use $scope*/

.controller('UserController', function($http) {
    var vm = this;

    vm.total_rows = 90;
})
.controller('GroupController', function($http) {
    var vm = this;

    vm.total_rows = 60;
});

我想对所有页面只使用一个分页指令,就像这样

.directive('datasPagination', function() {

  return {
      restrict: 'E',
      template: '<ul class="pagination"><li>{{total_rows}}</li></ul>'
  }

});

如果我使用不同别名的Controller As Syntax并且我不使用$ scope,如何在我的指令中获得{{total_rows}}? 。上面,我用&#34; usr&#34;和&#34; grp&#34;作为别名

此致

1 个答案:

答案 0 :(得分:2)

尝试隔离指令范围,当您想构建可重用的组件时,这很有用......

&#13;
&#13;
APP.directive('datasPagination', function() {

  return {
    restrict: 'E',
    scope: {
      rows: "="
    },
    template: '<ul class="pagination"><li>{{rows}}</li></ul>'
  }

});
&#13;
<!-- where alias is the alias defined in controllerAs syntax -->
<data-pagination rows="alias.total_rows" />
&#13;
&#13;
&#13;

further reading here

相关问题