imbagila
imbagila

Reputation: 513

How to change ng-model dynamically with ng-click controller function

I'm new in AngularJS, and I try to change ng-model dynamically based on ng-click parameter function. So here is the code :

HTML :

<ul class="dropdown-menu" id="search-dropdown-menu">
    <li>
        <a href="javascript:void(0);" data-ng-click="searchFilter('jurusan', '-')">
            -
        </a>
    </li>
    <li>
        <a href="javascript:void(0);" data-ng-click="searchFilter('jurusan', 'kd_jurusan')">
            Kode Jurusan
        </a>
    </li>
    <li>
        <a href="javascript:void(0);" data-ng-click="searchFilter('jurusan', 'nama_jurusan')">
            Nama Jurusan
        </a>
    </li>
</ul>

<input type="text"
    class="form-control input-sm"
    data-ng-model="search"
    placeholder="Cari data . . ."
    autofocus />

AngularJS :

/**
 *  Controller
 */
    akuaApp.controller('akuaController', function($scope)
    {
        $scope.searchFilter = function (model, value) {
            $scope.search = 'search.'+value;
        };
    });

And that is being changed is on the input value, not the data-ng-model value. I tried to read all tutorials but I still did not get it about this. Can someone help me, thanks :)

EDITED : (sorry for my bad English)

So what I want is, lets say I click this

<a href="javascript:void(0);" data-ng-click="searchFilter('jurusan', 'nama_jurusan')">
    Nama Jurusan
</a>

After I click that, I want this data-ng-model attribute value, from search into search.nama_jurusan. nama_jurusan is from searchFilter second parameter.

Note : I was put the data-ng-controller and data-ng-app before

Upvotes: 1

Views: 785

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222522

var app = angular.module('DemoApp', [])
app.controller('akuaController', function($scope) {

  $scope.searchFilter = function(model, value) {
    $scope.search = 'search.' + value;
    alert($scope.search);
  };
});
<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app="DemoApp"    ng-controller="akuaController">
  
 <ul class="dropdown-menu" id="search-dropdown-menu">
    <li>
        <a href="javascript:void(0);" data-ng-click="searchFilter('jurusan', '-')">
            -
        </a>
    </li>
    <li>
        <a href="javascript:void(0);" data-ng-click="searchFilter('jurusan', 'kd_jurusan')">
            Kode Jurusan
        </a>
    </li>
    <li>
        <a href="javascript:void(0);" data-ng-click="searchFilter('jurusan', 'nama_jurusan')">
            Nama Jurusan
        </a>
    </li>
</ul>

<input type="text"
    class="form-control input-sm"
    data-ng-model="search"
    placeholder="Cari data . . ."
    autofocus />
  
 </body>

</html>

Upvotes: 1

Related Questions