2

I have one simple nav-bar ,In which when I click on tab I need to pass value of tab into a method.
I tried this but am getting undefined.

<body ng-app="myApp" ng-controller="myCtrl">
<div>
<ul><li ng-click="displayValue(home)"><a data-toggle="tab" href="#"><span class="tabName" ng-model="home">Home</span></a></li></ul>
</div>
</body>

<script>
var app=angular
            .module('myApp', [])
            .controller('myCtrl', function ($scope) {
                $scope.displayValue= function (val) {
                    alert(val)
                }
            })
 </script>

Here I need to pass home into a method.

1
  • show us your controller Commented Jun 16, 2017 at 5:20

4 Answers 4

2

You cant pass the text as argument from the view. If you have define ng-model, then you can directly access it in the controller. Here is the updated code. I think it will clear all your doubt.

var app=angular.module('myApp', [])
    app.controller('myCtrl', function ($scope) {
        $scope.home = "Home";
        $scope.displayValue = function (val) {
            alert($scope.home);
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
<ul><li ng-click="displayValue(home)"><a data-toggle="tab" href="#"><span class="tabName" ng-model="home">{{home}}</span></a></li></ul>
</div>

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

3 Comments

ok If I have multiple tab means?then what would be the solution?
Are the multiple tabs being produced with ng-repeat?
@krish Here is the updated fiddle Which will clear your doubt regarding multiple tabs. jsfiddle.net/biswajit_rout/f1o0jj58/6
1

Just Pass your data as string ng-click="displayValue('home').
For multiple tabs also you can pass like this.

1 Comment

Thanks ..I got it
1

Ok first you need to reference Home as a scope variable using double curly brackets "{{home}}". Then you need to define this scope variable in your controller. Here is the code: https://jsfiddle.net/AKMorris/ogpcvze7/

HTML

<div data-ng-app='myApp' data-ng-controller='myCtrl'>
<li ng-click="displayValue()"><a data-toggle="tab" href="#"><span class="tabName" ng-model="home">{{home}}</span></a></li>
</div>

Javascript

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
        $scope.home = "Home";
        $scope.displayValue = function (home)
    {
        console.log($scope.home);
    }
});

Comments

1

You should use your function name properly, it should be goSearch instead of displayValue

<li ng-click="goSearch(home)">

EDIT

If you want to pass the ng-model variable to a function, you need to have the value initialized.

 $scope.home = "Home";
 $scope.goSearch = function (val) {
            alert($scope.home);
 }

DEMO

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.home = "Home";
     $scope.goSearch = function (home)
    {
        console.log($scope.home);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app='myApp' data-ng-controller='myCtrl'>
<li ng-click="goSearch()"><a data-toggle="tab" href="#"><span class="tabName" ng-model="home">{{home}}</span></a></li>
</div>

1 Comment

ng-model="home" on a element? I can't see how this will alert anything other than undefined

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.