I have a directive that will use ng-model to expose its value.
The question is that this directive have internal components that will also mess the scope, so I need to isolate its scope, but still preserve ng-model bind to outside world.
How would I achieve this?
This is the directive:
angular.module('app', []).directive('myDirective', function() {
return {
restrict: 'E',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
// do stuff using ngModel controller
},
replace: true,
template: '<div><input ng-model="userInput" /></div>'
};
});
<my-directive ng-model="prop"></my-directive>
As you can see, the directive must expose prop as its value, but should not expose userInput in the parent scope, defined in <input ng-model="userInput"/>.
How would I be able to make only prop available in the parent scope?
propinside the directive? Will it become be part of a field element? Also, thengModelyou show -- is that intended forproporuserInput? - Mark Rajcokng-models. The one from input will be the user input. My directive controller will$watchfor changes in it, complement and updatepropusingngModelController.$setViewValue(). I know I could listen to input events and avoid the internalng-model, but in future, I'll have more elements sharing the same internal scope properties. So I'd like to have an isolated scope where internal elements can play, but still would like to haveng-model="prop"bound to parent scope. I'll also use validity feature from externalng-model. - Caio Cunha