1

I made tabs using angular js, problem is that i need to set value sel=1 at page load to show first content when page load. How can i resolve this.

<div ng-app="">
    <div ng-controller="myController">
        <ul>
            <li><a href ng:click="sel=1">First</a></li>
            <li><a href ng:click="sel=2">Second</a></li>
            <li><a href ng:click="sel=3">Third</a></li>
        </ul>

        <div ng:show="sel == 1">
            This is first content...
        </div>

        <div ng:show="sel == 2">
            This is second content...
        </div>

        <div ng:show="sel == 3">
            This is third content...
        </div>
    </div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
<script type="text/javascript" ng:autobind src="http://code.angularjs.org/0.9.19/angular-0.9.19.js"></script>
<script>
    function myController($scope) {
        $scope.sel = 1;
    }
</script>

2 Answers 2

1

You can try explicitly defining the Angular app and controller.

<div ng-app="app">
    <div ng-controller="myController">
        <ul>
            <li><a href ng-click="sel=1">First</a></li>
            <li><a href ng-click="sel=2">Second</a></li>
            <li><a href ng-click="sel=3">Third</a></li>
        </ul>

        <div ng-show="sel == 1">
            This is first content...
        </div>

        <div ng-show="sel == 2">
            This is second content...
        </div>

        <div ng-show="sel == 3">
            This is third content...
        </div>
    </div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
<script type="text/javascript" ng:autobind src="http://code.angularjs.org/0.9.19/angular-0.9.19.js"></script>
<script>
    var app = angular.module('app', []);
    app.controller('myController', function ($scope) {
        $scope.sel = 1;
    });
</script>

Try the code above.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to set the NG-Init event, if this is to early, wrap with $(window).load event.

 <div ng-controller="myController" ng-init="initEvent()">
        <ul>
            <li><a href ng:click="sel=1">First</a></li>
            <li><a href ng:click="sel=2">Second</a></li>
            <li><a href ng:click="sel=3">Third</a></li>
        </ul>

        <div ng:show="sel == 1">
            This is first content...
        </div>

        <div ng:show="sel == 2">
            This is second content...
        </div>

        <div ng:show="sel == 3">
            This is third content...
        </div>
    </div>

<script>
    function myController($scope) {
        $scope.sel = 1;
        $scope.initEvent = function()
        {
           //init event for controller
           //if we're to early here try:
           $(window).load(function()
           {

           });
          // or
             $(document).ready(function()
           {

           });
        }
    }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.