angularjs - Directive, Link Function, Array, DataBinding Order of things is ambiguous -


i have problem challenging, don't know how define, i'm going give example , able explain doing wrong. html is:

<tr ng-repeat="object in objects">     <td>         <div table-checkbox row-index="{{$index}}" bind-model="selectedchecks[$index]" selected-checks="selectedchecks">         </div>     </td> </tr> 

the selectedchecks array define controller this: $scope.selectedchecks = {} , result of markup column of checkboxes here directive's code:

return {     restrict: 'ea',     scope: {         rowindex: '@',         selectedchecks: '=',         bindmodel: '='     },     template:'<input type="checkbox" ng-model="bindmodel" ng-change="checkit(rowindex)">',       link: function(scope, elem, attrs) {        scope.checkit = function(i){            alert(scope.bindmodel + " " + scope.selectedchecks[i]);         };     } }; 

the problem when first checkbox checked alert output: "true undefined" form second check/uncheck of checkbox , own output be: "true false" , "false true"

my expectation 2 variables same since should have point same value since: scope.bindmodel sent directive selectedchecks[$index] , scope.selectedchecks[i] should same. please me understand doing wrong here. seems checkit functions runs before angular data-binding between checkboxes , array.

it seems checkit functions runs before angular data-binding between checkboxes , array.

no, checkit function not running before angular dose data binding. reason why true undefined @ first time check first 1 $scope.selectedchecks = {} empty. that's mean there no property like:

$scope.selectedchecks = {     0: false }; 

so, time check first checkbox, ng-model="bindmodel" update $scope.selectedchecks = {} so:

$scope.selectedchecks = {   0: true } 

and same other checkboxes.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -