user2301515
user2301515

Reputation: 5107

angularjs and calling function in the same object

I use two simple form with angularjs

<form ng-controller="ctrs.ctr1">
    <input type="text" placeholder="Name" ng-model="name" />{{getName()}}
</form>

and

<form ng-controller="ctrs.ctr2">
    <input type="text" placeholder="Name" ng-model="name" />{{getName()}}
</form>

and s small script for e.g showing name twice

var ctrs = {
    nameTwoTimes: function(name) {
        return name+" "+name;
    },
    ctr1: function($scope, $timeout) {
        $scope.name = '';
        $scope.getName = function() {
            return $scope.name+" "+$scope.name;
        };
    },
    ctr2: function($scope, $timeout) {
        $scope.name = '';
        $scope.getName = function() {
            this.nameTwoTimes($scope.name);
        };
    }
};

How to use the function nameTwoTimes, that in calling it is not "undefined"? Thanks.

Upvotes: 0

Views: 42

Answers (1)

Tom
Tom

Reputation: 44821

Like this:

var nameTwoTimes: function(name) {
    return name+" "+name;
};
var ctrs = {        
    ctr1: function($scope, $timeout) {
        $scope.name = '';
        $scope.getName = function() {
            return $scope.name+" "+$scope.name;
        };
    },
    ctr2: function($scope, $timeout) {
        $scope.name = '';
        $scope.getName = function() {
            nameTwoTimes($scope.name);
        };
    }
};

Upvotes: 1

Related Questions