bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

tabset, active property on tab - undefined is not a function

Open MannyC opened this issue 13 years ago • 47 comments

I get this when I try to use the active property on a tab, example:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="bootstrap.min.css">
        <script src="angular.js">  </script>
        <script src="ui-bootstrap-tpls-0.4.0.js"> </script>
        <script src="angular-test.js"> </script >
    </head>
    <body ng-app="angular-test">
        <div ng-controller="AngularTestController">
            <tabset>
                <tab heading="Tab 1" active="true"> test 1 </tab>
                <tab heading="Tab 2"> test 2 </tab>
            </tabset>
        </div>
    </body>
</html>

angular-test.js:

angular.module('angular-test', ['ui.bootstrap']);
function AngularTestController($scope) {
  //nothing yet
}

Stack trace: TypeError: undefined is not a function at postLink (file:///C:/proj/angular-test/ui-bootstrap-tpls-0.4.0.js:2374:11) at nodeLinkFn (file:///C:/proj/angular-test/angular.js:4406:13) at delayedNodeLinkFn (file:///C:/proj/angular-test/angular.js:4559:11) at compositeLinkFn (file:///C:/proj/angular-test/angular.js:4003:15) at publicLinkFn (file:///C:/proj/angular-test/angular.js:3920:30) at file:///C:/proj/angular-test/angular.js:4009:30 at new ngDirective.controller (file:///C:/proj/angular-test/angular.js:14046:5) at invoke (file:///C:/proj/angular-test/angular.js:2902:28) at Object.instantiate (file:///C:/proj/angular-test/angular.js:2914:23) at file:///C:/proj/angular-test/angular.js:4805:24

  • MannyC avatar Jul 02 '13 16:07 MannyC
  • @MannyC I think that the active attribute needs to point to an assignable variable - it wouldn't make sense otherwise as having a constant would mean that the first tab is always selected and you couldn't change this selection.

    Could you use a boolean variable on a scope and check how i goes?

    pkozlowski-opensource avatar Jul 02 '13 16:07 pkozlowski-opensource

    Thanks, that was it.

    I was misunderstanding how this would work. I originally had a method call in there, true was just for the example, but the method call is also not ... watchable? I see how this is supposed to work now.

    MannyC avatar Jul 02 '13 16:07 MannyC

    @pkozlowski-opensource Pawel, does that mean the tabs always needs to be created dynamically to make selection works programatically.

    I was planning to do something like

    <tab heading="Result1" active="_tab_active == 'Result1'"></tab>
    <tab heading="Result2" active="_tab_active == 'Result2'"></tab>
    <tab heading="Result3" active="_tab_active == 'Result3'"></tab>
    

    so, just set _tab_active to whatever value.

    With your suggestion, I cannot do this with single variable. That is each tab need an assignable boolean variable in scope.

    Any better way. Thanks.

    bsr203 avatar Aug 02 '13 00:08 bsr203

    bump.

    ecolman avatar Aug 05 '13 18:08 ecolman

    You can't do this with a single variable since the active attribute only resolves to truthy/falsey values, but doesn't seem to resolve expressions (like what you are doing). So if you're generating the tabs dynamically (in an ng-Repeat):

    <tab active="tabs[$index].active"></tab>
    

    And your model would be an array of tab objects.

    $scope.tabs = [{
        ...
       active: true,
        ...
    }];
    

    In short, no a single variable cannot store the active state of more than one tab.

    hallister avatar Aug 05 '13 19:08 hallister

    The culprit to this seems to be:

    getActive = $parse(attrs.active);
    

    Because the tab directive is an isolate scope, we can't $eval it against the controller scope so the line you are passing in, '_tab_active' would always render as undefined (I think)... which means you probably never get an active tab. Whereas passing in a variable value that resolves to true or false appears to keep the two-way binding, so changing it works. In other words, I don't think it's possible (or easily fixable) to set active="{{expression}}" the way you are.

    Anyone with a little more knowledge of the tabs directive, feel free to correct me if I'm wrong.

    hallister avatar Aug 05 '13 19:08 hallister

    You can only do it with an assignable value. We are thinking about adding an ng-model attribute to the tabset in the future to make this easier.

    ajoslin avatar Aug 06 '13 01:08 ajoslin

    Sounds like people are looking for an initial-value attribute or similar.

    MannyC avatar Aug 06 '13 08:08 MannyC

    How does one would handle routing in tabs ? If my tabs route to several different controllers, how would I handle that 'tabs' scope variable handling my tabs state ? Do I have to make a kind of navigation service for this ? or is there a better way ? (new to angular, still fighting ^^)

    mhicauber avatar Nov 18 '13 16:11 mhicauber

    Just wasted 1h on this :( It was totally counter-intuitive to not be able to use an expression for active's value. Had only 2 tabs and the intention was to have one var to hold the state of which tab was active by setting for the first tab active="isFirstTabActive" and for the second tab active="!isFirstTabActive" which ofc failed probably for the reason mentioned above by hall5714.

    tkalfigo avatar Feb 11 '14 14:02 tkalfigo

    Agreed, I expected to be able to use an expression in the active attribute.

    (I'm on 0.8.0 with Bootstrap 2.3, so this may have changed by now)

    More than that I expected to supply the active tab in one place, e.g.

    <tabset active="'foo'">
      <tab name="foo">...</tab>
      <tab name="bar">...</tab>
    </tabset>
    

    Because I couldn't do that I was going to do the equivalent on each tab (active="activeTab == 'foo'", active="activeTab == 'bar'" etcetera).

    The current approach seems very un-angular. I get that it's so you can set the value with setActive(scope.$parent, active) when the active tab changes, but that can only work when you're setting a boolean value which means you have to have a single boolean variable for each tab (which is useless). People can hook that up themselves in the case when it matters, the rest of the time this is getting in the way.

    wmadden avatar Feb 15 '14 21:02 wmadden

    When the user clicks the button in the first tab, it should show the second tab. I tried setting like

    1. tabs[0].active = true
    2. $scope.isFirst = true; tabs[0].active = $scope.isFirst;

    But no luck. Could you please help me to achieve this?

    theprince25 avatar Feb 18 '14 17:02 theprince25

    @theprince25 see the buttons on the top of this plnkr: http://plnkr.co/edit/e89cV66bCupLiGfVy5QL?p=preview

    Sourced from the angular bootstrap docs. Make sure you don't have scoping issues.

    MannyC avatar Feb 18 '14 18:02 MannyC

    I did this (my tabs show different forms depending on the type of question the user's creating):

    <span ng-init="isMultipleChoice = question.response_type == 'multiple_choice'"></span>
    <span ng-init="isShortAnswer = question.response_type == 'short_answer'"></span>
    <span ng-init="isPoll = question.response_type == 'poll'"></span>
    
    <tabset>
      <tab active="isMultipleChoice">...</tab>
      <tab active="isShortAnswer">...</tab>
      <tab active="isPoll">...</tab>
    </tabset>
    

    Now Angular UI can mess around with variables all it likes and I get to use an expression to decide which tab is visible.

    wmadden avatar Feb 18 '14 18:02 wmadden

    @wmadden Thanks. It works perfectly on button clicks. But the problem is, the normal behavior of Tab gets collapsed. I mean, when I click the tab, the previous tab is also in active state. Please see the attachment tab_error

    code

    Faced some issue while posting html tags, so copied as image.

    Thanks.

    theprince25 avatar Feb 19 '14 05:02 theprince25

    The active attribute is going to change for the next version. You all could try to use master with what you expected it to work and then also your workarounds.

    Foxandxss avatar Feb 19 '14 11:02 Foxandxss

    @theprince25 the problem in your code is the active="selectedTab == 'foo'" expressions. At the moment the tabset directive expects those expressions to just hold the name of a scope variable so that when you change tabs the directive can assign to it, rather than just reading the value out to decide if the tab is active.

    So to make your code work you'd need to change it to look like this. FYI use three backticks to do a code block, no HTML required.

    <tabset>
      <tab active="selectedTabIsSearch">...</tab>
      <tab active="selectedTabIsPreview">...</tab>
      <tab active="selectedTabIsEdit">...</tab>
    </tabset>
    ...
    $scope.searchTab = function() {
      $scope.selectedTabIsSearch = true;
      $scope.selectedTabIsEdit = false;
      $scope.selectedTabIsPreview = false;
    }
    $scope.searchTab = function() {
      $scope.selectedTabIsEdit = false;
      $scope.selectedTabIsEdit = true;
      $scope.selectedTabIsPreview = false;
    }
    $scope.searchTab = function() {
      $scope.selectedTabIsPreview = false;
      $scope.selectedTabIsEdit = false;
      $scope.selectedTabIsPreview = true;
    }
    

    A better approach might be to use master as @Foxandxss suggests.

    wmadden avatar Feb 19 '14 15:02 wmadden

    @wmadden Thanks, but the above code doesn't work on button clicks. I have 3 tabs. If the user clicks a button in the first tab, it should load some data in the second tab and that 2nd tab should be active. What is master? I googled but couldn't get much idea on it.

    theprince25 avatar Feb 19 '14 16:02 theprince25

    @theprince25 he means using the current master branch at https://github.com/angular-ui/bootstrap rather than one of the releases like you're probably using. The implication is that the master branch currently incorporates the new "active" functionality that hasn't been released yet.

    MannyC avatar Feb 19 '14 16:02 MannyC

    Atlast I found solution for that issue. Every tab needs, active="tabs[0].active" property.

    <button class="btn" ng-click="tabs[0].active =true">Search</button>
    <button class="btn" ng-click="tabs[1].active =true">Preview</button>
    <button class="btn" ng-click="tabs[2].active =true">Edit</button>
    
    <tab heading="Search" active="tabs[0].active">
    <tab heading="Preview" active="tabs[1].active">
    <tab heading="Edit" active="tabs[2].active">
    

    This works perfectly. Thanks.

    theprince25 avatar Feb 20 '14 16:02 theprince25