Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <my-directive data-allow-something="false">
- ... this works as expected as no default value should be set in this case ...
- </my-directive>
- <my-directive>
- ... but in this case i'd like allowSomething to equal true ...
- </my-directive>
- angular.module('myApp').directive('myDirective') {
- ...
- controller: function($scope){
- if (!$scope.allowSomething)
- $scope.allowSomething = true;
- },
- ....
- scope: function(){
- allowSomething: '@'
- }
- ...
- }
- controller: function($scope){
- if (angular.isUndefined($scope.allowSomething))
- $scope.allowSomething = true;
- }
- <my-directive allow-something="false"></my-directive>
- <my-directive></my-directive>
- angular
- .module('app')
- .directive('myDirective', function() {
- var defaults = {
- allowSomething: true
- };
- return {
- restrict: 'E',
- templateUrl: '',
- scope: {},
- compile: function() {
- return {
- pre: function(scope, el, attrs) {
- scope.allowSomething = attrs.allowSomething || defaults.allowSomething;
- },
- post: function(scope, el) {
- // this is link
- // scope.allowSomething = default or whatever you enter as the attribute "false"
- }
- };
- }
- };
- }
- scope : true,
- link : function(scope, element, attrs){
- scope.allowSomething = $parse(attrs.allowSomething)(scope) || true;
- console.log(scope)
- }
- <body>
- <my-directive></my-directive>
- <!-- <my-directive allow-something="false"></my-directive> -->
- </body>
- angular.module('myApp', [])
- .directive('myDirective', function() {
- return {
- scope: {},
- link: function(scope, element, attrs) {
- scope.allowSomething = ('allowSomething' in attrs) ? attrs.allowSomething : true;
- },
- template: '<div>allow:{{ allowSomething }}</div>'
- };
- });
- angular.module('myApp', [])
- .directive('myDirective', function() {
- return {
- scope: {
- allowSomething: '@'
- },
- controller: function($scope, $timeout) {
- $timeout(function() {
- if (angular.isUndefined($scope.allowSomething)) {
- $scope.allowSomething = true;
- }
- });
- },
- template: '<div>allow:{{ allowSomething }}</div>'
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement