Hassanation
Hassanation

Reputation: 886

binding to controller object in Angular

I'm new to angular, trying to bind an an element's content into the controller's Scope to be able to use it within another function:

here is the scenario am working around:

I want the content of the <span> element {{y.facetName}} in

<span ng-model="columnFacetname">{{y.facetName}}</span>

to be sent to the controller an be put in the object $scope.columnFacetname in the controller

Here is a snippet of what I'm working on:

<div ng-repeat="y in x.facetArr|limitTo: limit track by $index ">
        <div class="list_items panel-body ">
            <button class="ButtonforAccordion" ng-click="ListClicktnColumnFilterfunc(); onServerSideButtonItemsRequested(ListClicktnColumnFilter, myOrderBy)">
                <span>{{$index+1}}</span>
                <span ng-model="columnFacetname">{{y.facetName}}</span>
                <span>{{y.facetValue}}</span>
            </button>
        </div>
</div>

angular.module('mainModule').controller('MainCtrl', function($scope, $http) {
                $scope.columnFacetname = "";
                $scope.ListClicktnColumnFilter = "";
                $scope.ListClicktnColumnFilterfunc = function() {
                    $scope.ListClicktnColumnFilter = "\":\'" + $scope.columnFacetname + "\'";
                };
            }

the problem is that the $scope.ListClicktnColumnFilter doesn't show the $scope.columnFacetname within it, meaning that the $scope.columnFacetname is not well-binded.

Upvotes: 1

Views: 322

Answers (1)

Alhuck
Alhuck

Reputation: 1029

In your ng-click instead of calling two different function

ng-click="ListClicktnColumnFilterfunc(); onServerSideButtonItemsRequested(ListClicktnColumnFilter, myOrderBy)"

you can declare like this

ng-click="columnFacetname = y.facetName; onServerSideButtonItemsRequested(columnFacetname , myOrderBy)"

You are trying to pass that model to another function by assigning it to ListClicktnColumnFilter in your controller

By doing in this way, you can achieve the same thing.

I have done one plunker with sample array,

http://embed.plnkr.co/YIwRLWXEOeK8NmYmT6VK/preview

Hope this helps!

Upvotes: 2

Related Questions