如何在Angular Js指令中调用部分控制器

时间:2014-09-19 09:58:52

标签: angularjs angular-directive

我已经为Tab创建了一个指令,它适用于静态控制器但是当添加控制器添加到任何一个模板时它都没有显示任何内容。

我的Tab指令在这里

angular.module('nsTab', [])
    .directive('nsTabset',  function() {

    return {
        restrict: 'E',
        scope: {
            tabs: '=tabs'
        },
        transclude: true,
        link: function($scope, element, attributes) {
              $scope.currentTab = $scope.tabs[0].url;

        },
        controller: function($scope, $element){

            $scope.activateTab = function(tab){

                $scope.currentTab  = tab.url;
            }
        },
        templateUrl: 'modules/common/views/tabset.html'
    };
});

本地范围

$scope.tabs = [
    {name: 'Headends', url: 'modules/lineup/views/ShowSystem/head.html', isActive: true},
    {name: 'Contacts', url: 'modules/lineup/views/ShowSystem/contacts.html'}
]
html中的

指令

<ns-tabset tabs="tabs"></ns-tabset>

head.html的内容

<div ng-controller="HeadCtrl">{{value}}</div>

SystemHeadendCtrl.js

angular.module('myMod').controller('HeadCtrl', function($scope, Restangular) {
    $scope.headendList = function(){
        $scope.myData = [{name: "Moroni", age: 50},
                         {name: "Tiancum", age: 43},
                         {name: "Jacob", age: 27},
                         {name: "Nephi", age: 29},
                         {name: "Enos", age: 34}];
        $scope.value= 10;
        $scope.gridOptions = { data: 'myData' };
    }
});

当该标签处于活动状态时,它在head.html中没有显示任何内容。

1 个答案:

答案 0 :(得分:0)

问题出在代码中,我正在共享选项卡控件的代码

http://plnkr.co/edit/SAeRZDp1vg7xxmCdG6Vv

var app = angular.module('plunker', ['nsTab']);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
   $scope.tabs = [
                {name: 'Head', url: 'head.html', isActive: true},
                {name: 'Tail', url: 'tail.html'},

            ]

});


angular.module('nsTab', [])
    .directive('nsTabset',  function() {

    return {
        restrict: 'E',
        scope: true,
        transclude: true,
        link: function($scope, element, attributes) {
              $scope.currentTab = $scope.tabs[0].url;

        },
        controller: function($scope, $element){

            $scope.activateTab = function(tab){

                $scope.currentTab  = tab.url;
            }
        },
        templateUrl: 'tabset.html'
    };
});

app.controller('MyCtrl', function($scope) {
 $scope.test = 'testing';
});