Chen-Tsu Lin
Chen-Tsu Lin

Reputation: 23214

AngularJS: What is the different between pass scope variable as arguments in view and use directly in controller?

What is the different, it's advantages and disadvantage

Pass scope variable as arguments in View:

<form ng-submit="login(credentials)">

    <input type="text"     ng-model="credentials.username">
    <input type="password" ng-model="credentials.password">

</form>

var LoginController = function($scope) {

    var login = function(credentials) {

    };

    $scope.credentials = {
        username: '',
        password: ''
    };

    $scope.login = login;
}

Or Directly use scope variable in Controller:

<form ng-submit="login()"> <!-- no argument -->

    <input type="text"     ng-model="credentials.username">
    <input type="password" ng-model="credentials.password">

</form>

var LoginController = function($scope) {

    var login = function() {
        // $scope.credentials
    };

    $scope.credentials = {
        username: '',
        password: ''
    };

    $scope.login = login;
}

Upvotes: 0

Views: 548

Answers (2)

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20741

Since Angular JS is having two-way data binding feature, you don't need to send credentials on submit like as shown below

<form ng-submit="login(credentials)"> 

Take a look at this example DEMO, As soon as you type something within the username or password text fields the datas get binded within the variable, so at the time of submit you don't need to send the credentials

Upvotes: 1

bto.rdz
bto.rdz

Reputation: 6720

in that case none, because it is only one object, I have not really alot of experence in anguar but I have noticed that you pass parametrs usually when you use ng-repeat with an ajax call so you can let angular know wich object will perform your function.

something like:

var LoginController = function($scope, $resource) {

$scope.credentials = $resource('/api/credentials')

$scope.login = function(credential) {
    alert(credential.name)
};;
}

then your html:

<table>
    <tr ng-repeat="credential in credentials">
        <td>credential.name</td>
        <td ng-click="login(credential)"></td>
    </tr>
</table>

Upvotes: 1

Related Questions