This repository has been archived by the owner on May 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 356
Initial version of the ui-input directive - RFC #191
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
angular.module('ui.directives') | ||
.directive('uiInput', ['$compile', '$http', '$templateCache', 'ui.config', function ($compile, $http, $templateCache, uiConfig) { | ||
|
||
uiConfig.input = uiConfig.input || {}; | ||
|
||
var idUid = ['0', '0', '0']; | ||
var nameUid = ['0', '0', '0']; | ||
|
||
/* | ||
Copyright (c) 2010-2012 Google, Inc. http://angularjs.org | ||
This function is slightly modified version of the nextUid() function found in AngularJS code base | ||
*/ | ||
function nextUid(uid) { | ||
var index = uid.length; | ||
var digit; | ||
|
||
while (index) { | ||
index--; | ||
digit = uid[index].charCodeAt(0); | ||
if (digit == 57 /*'9'*/) { | ||
uid[index] = 'A'; | ||
return uid.join(''); | ||
} | ||
if (digit == 90 /*'Z'*/) { | ||
uid[index] = '0'; | ||
} else { | ||
uid[index] = String.fromCharCode(digit + 1); | ||
return uid.join(''); | ||
} | ||
} | ||
uid.unshift('0'); | ||
return uid.join(''); | ||
} | ||
|
||
/* | ||
Copyright (c) 2010-2012 Google, Inc. http://angularjs.org | ||
This function is slightly modified version of the snake_case() function found in AngularJS code base | ||
*/ | ||
function snake_case(name, separator) { | ||
return name.replace(/[A-Z]/g, function (letter, pos) { | ||
return (pos ? separator : '') + letter.toLowerCase(); | ||
}); | ||
} | ||
|
||
return { | ||
restrict:'EA', | ||
priority:10000, | ||
terminal:true, | ||
scope:true, | ||
require:'?uiForm', | ||
compile:function compile(tElement, tAttrs, transclude, uiForm) { | ||
|
||
var model = tAttrs.ngModel, input = { | ||
id:tAttrs.id || 'input' + nextUid(idUid) | ||
}; | ||
|
||
return function (scope, element, attrs) { | ||
|
||
//infer a field type from template's tag name (can be one of ui-input, ui-select, ui-textarea) | ||
$http.get(scope.$eval(attrs.src), {cache:$templateCache}).success(function (response) { | ||
|
||
element.html(response); | ||
|
||
var inputEl = element.find('[ng-transclude]'); | ||
angular.forEach(tAttrs, function (value, key) { | ||
if (key.charAt(0) !== '$' && ['src','uiInput'].indexOf(key) === -1) { | ||
inputEl.attr(snake_case(key, '-'), value); | ||
} | ||
}); | ||
|
||
//prepare validation messages | ||
input.validation = angular.extend({}, uiConfig.input.validation, scope.$eval(tAttrs.validation)); | ||
|
||
//expose model to a field's template | ||
scope.$input = input; | ||
$compile(element.contents())(scope); | ||
scope.$field = inputEl.controller('ngModel'); | ||
}); | ||
}; | ||
} | ||
}; | ||
}]); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think $compile errors because of the ng-transcludes. It doesn't know what to do. You'll have to manually transclude the elements with ng-transclude attribute, then remove them before $compiling.
OR alternatively, you could try putting in all the html during compile phase, and it might know how to use the transcludes. But you don't scope during compile phase, so that would hard to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Touché, I guess I just figured that ng-transclude was not actually a
directive but a subset.
On Oct 31, 2012 7:46 PM, "Andy Joslin" notifications@github.com wrote: