angularjs - Passing scope object to Angular Directive via $compile -
i know if have directive in html, can pass objects $scope it:
<mydirective some-data="somedata"></mydirective>
...where somedata
might json object.
is possible pass reference somedata
if create directive dynamically , use $compile
?
$scope.somedata = { firstname: 'john', lastname: 'doe' }; var link = $compile("<mydirective some-data='???'></mydirective>"); var content = link($scope); $(body).append(content);
yes can pass object to
var myapp = angular.module('myapp',[]); myapp.directive('passobject', function() { return { restrict: 'e', scope: { obj: '=' }, template: '<div>hello, {{obj.prop}}!</div>' }; }); myapp.controller('myctrl', function ($scope) { $scope.obj = { prop: "world" }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script> <div ng-controller="myctrl"> <pass-object obj="obj"></pass-object> </div>
var myapp = angular.module('myapp',[]); myapp.directive('passobject', function() { return { restrict: 'e', scope: { obj: '=' }, template: '<div>hello, {{obj.prop}}!</div>' }; }); myapp.controller('myctrl', function ($scope) { $scope.obj = { prop: "world" }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script> <div ng-controller="myctrl"> <pass-object obj="obj"></pass-object> </div>
directive
Comments
Post a Comment