javascript - Send data from main html page controller to a directive controller -


i trying communicate between controllers , directive. have studied few blogs , searched stackoverflow, there wonderful concepts these topics. case specific problems. tried solve problem in style found no luck.

my problem is.. need send array directive. array defined in controller of main html file. , html page has directive too. directive has own controller job. need send array directive controller processing , array has 2 way binded changes in 1 side can reflected in side.

i dealing kind of situation lately , figured out way deal it. can use procedure send data , process in directive controller. first make controller in main page , define array want...

app.controller("ctrl", function($scope) {     $scope.scope = $scope; //this transfer current scope directive     $scope.array = [{         "a": "vfdxvf",         "b": "sdc"     }, {         "a": "vfdxvf",         "b": "sdc"     }, {         "a": "vfdxvf",         "b": "sdc"     }, {         "a": "vfdxvf",         "b": "sdc"     }]; }); 

above in code, $scope.scope = $scope create reference of controller scope.

and define directive this. bind values want. , change in array on side, reflect on other side well.

here dummy code..

app.directive("somedirective", function() {     return {         restrict: "ea",         scope {             array: "=", // 2 way binding of array..             scope: '=' // collecting scope of controller came         },         templateurl: "./templates/test.html",         controller: function($scope) {             $scope.newarray = $scope.scope.array;             //do useful coding array...          }     } }); 

and html main page write this..

<some-directive scope="scope" array="array"></some-directive> 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -