0

I am using two controllers and a factory service to get the data from it. I want to filter the data in the second controller by input 'ng-model'. So i have written input ng-model in both the controllers (check index.html). Its working if i tried to enter the input data in the second controller input field, but its not working if i try filtering from first controller input ng-app.

index.html

<!DOCTYPE html>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>            
    <script src="scripts/controller.js"></script>   
</head>


<body ng-app="app" ng-controller="appCtrl">     
    <div style="float: left; width: 300px;">            
            <div ng-controller="checkBoxModuleCtrl">                    
                <ul>
                    <li ng-repeat="item in chkBoxList" style="list-style: none;">
                        <input type="checkBox" value="{{item}}" ng-click="checkBoxClickHandler($index, $event)"> {{item}}
                    </li>                   
                </ul>                   
                <input type="text" ng-model="myText" />
            </div>          
    </div>

    <!--<input type="button" ng-click="checkBoxClickHandler()" value="Click Me!"> </input>-->

    <div style="float: left; width: 400px; height: 600px; overflow-y: scroll;" >

        <div>
            <div ng-controller="panelCtrl"> 
              <input type="text" ng-model="myText" />                       
                <ul>
                    <li ng-repeat="panelItem in panelData|filter:myText" style="list-style: none;">
                        <div>                               
                            <b>Title </b/>: {{panelItem.name }}<br/>
                            <b>Channel-Type </b>: {{panelItem.type }}<br/>
                            <b>description </b>: {{panelItem.description }}
                        </div>
                        <hr weight="5" />
                    </li>

                </ul>       
            </div>

        </div> 
    </div>  
</body>

</html> 

controller.js

var app = angular.module("app", ["checkBoxserviceModule"]);


app.controller('appCtrl', function($scope){  

});

app.controller('checkBoxModuleCtrl', function($scope, externals){

    $scope.chkBoxList = [];

    $scope.init = function(){
        $scope.chkBoxList = externals.getCheckBoxList()
    };
    $scope.init();

    $scope.checkBoxClickHandler = function(itemIndex, event){
        alert(event.currentTarget.value + "will be handling click listener for check box" + itemIndex)
    }   
});

app.controller("panelCtrl", function($scope, externals){
    $scope.init = function(){
        $scope.panelData = externals.getPanelData();        
    };
    $scope.init();

    $scope.customFilter = function(panelItem){
        return panelItem.toUpperCase;

    }

});

var checkBoxserviceModule = angular.module("checkBoxserviceModule", []);
checkBoxserviceModule.factory("externals", function(){
    return{
        getCheckBoxList : function(){
            return [ "sports", "movies", "entertainment", "news" ]
        },

        getPanelData : function(){
        //alert("in services")
            return [
                    { 
                        "name":"cinmax", 
                        "type": "movies",
                        "description":"Some Tesxt"

                    }, 
                    { 
                        "name":"setmax", 
                        "type": "sports",
                        "description":"Some Tesxt"

                    }, 
                    { 
                        "name":"mtv", 
                        "type": "entertainment",
                        "description":"Some Tesxt"

                    }, 
                    { 
                        "name":"ibn news", 
                        "type": "news",
                        "description":"Some Tesxt"

                    }
                ];
            }


    };
});
Siva
  • 3,297
  • 7
  • 29
  • 35
  • Use a service instead of a factory, if you want to share data between the controllers. Services are singletons, factories are always new instances – 23tux Jul 10 '14 at 21:43

1 Answers1

0

Controllers use prototypical inheritance to share scope. So, you need to make sure the object you want inherited is an actual object in order for the reference to stay binded. If try to share a primitive from the scope, they will not be binded correctly and each controller will end up making it's own copy.

This might inherit correctly initially, but the binding will disconnect as soon as you change the value of the child's scope.number.

-ParentController
  -scope.number = 4
  -ChildController
    -scope.number //will not stay binded to parent's scope

This on the otherhand, if you create an object on the scope, will inherit a reference to the object, and will stay binded in the child controller.

-ParentController
  -scope.myObject.number = 4
  -ChildController
    -scope.myObject.number //will stay binded to parent's scope

Here is a better explanation: Why don't the AngularJS docs use a dot in the model directive?

Community
  • 1
  • 1
tommybananas
  • 5,718
  • 1
  • 28
  • 48