angularjs - angular directive scope inheritance/ isolation -
i have directive build dynamic table collection. have 2 types of columns checkbox , dropdowns. however, scope not resolving correctly @ directive level. getting 'undefined'. need bind drop down options controller.
html
<div ng-app="myapp"> <div ng-controller="myctrl"> <ui-table source="students"> <check-col title="passed" field="passed"/> <drop-down-col title="std" field="std" /> </ui-table> </div> </div>
angular js
var myapp = angular.module('myapp',[]); myapp.directive("uitable", function ($compile) { var generatetablehtml = function ($scope) { // header var html = "<table class='table table-condensed table-bordered table-responsive'><thead class='thead12'><tr class='active'>"; angular.foreach($scope.columns, function (col) { debugger; html += "<th scope='col'" + (col.cssclass ? " class='" + col.cssclass + "'" : "") + ">" + col.title + "</th>"; }); html += "</tr></thead><tbody>"; // body html += "<tr ng-repeat='item in datasource'>"; angular.foreach($scope.columns, function (col) { html += "<td" + (col.cssclass ? " class='" + col.cssclass + "'" : "") + ">"; if (col.type === columntype.check) { html += "<input type='checkbox' ng-model='item." + col.datafield + "'/>"; } else if (col.type === columntype.dropdown) { html += "<select ng-model='item." +col.datafield + "' ng-options = 'option.value option in dataoptions'></select>"; } html += "</td>"; }); html += "</tr>"; html += "</tbody></table>"; return html; }; return { restrict: "e", replace: true, transclude: true, scope: { datasource: "=source" }, controller: function ($scope) { $scope.columns = []; this.addcolumn = function (col) { $scope.columns.splice(0, 0, col); }; }, template: "<div ng-transclude></div>", compile: function () { return function ($scope, $elem) { $elem.html(generatetablehtml($scope)); $compile($elem.contents())($scope); }; } }; }); myapp.directive("checkcol", function () { return { require: "^uitable", restrict: "e", replace: true, scope: { title: "@title", cssclass: "@class", datafield: "@field" }, link: function ($scope, element, attrs, tablecontrol) { $scope.type = columntype.check; tablecontrol.addcolumn($scope); } }; }); myapp.directive("dropdowncol", function() { return { require: "^uitable", restrict: "e", replace: true, scope: { title: "@title", cssclass: "@class", datafield: "@field" }, link: function($scope, element, attrs, tablecontrol) { $scope.type = columntype.dropdown; tablecontrol.addcolumn($scope); } }; }); //myapp.factory('myservice', function() {}); var columntype = { check: 1, dropdown: 2 }; myapp.controller('myctrl', function($scope) { $scope.students = [{id:1, std:10, passed: true}, {id:2, std:9, passed: false}]; $scope.stds= [{value: 10, name: '10the std'},{value: 9, name: '9the std'},{value: 8, name: '8the std'}]; });
uprade angular ver. 1.2 , works fine
`http://jsfiddle.net/hb7lu/18635/`
Comments
Post a Comment