What is missing from your code is to compile html with a scope so that it knows who to bind models to.
Take a look at the Fork I made of yours plnkr.
But to make it clearer, take a look here:
Injects the $compile
in your directive because it will be responsible for compiling the html that you wrote dynamically in your directive.
app.directive('divCadastro', ['$compile', function($compile) {...
And when it’s time to do the element.append()
Compile your html by passing the scope of your controller:
element.append($compile(input)(scope.$parent));
Take a look at the documentation of $Compile when you can, but already in advance.
$compile({html a ser compilado})({escopo para fazer bind})
I’m calling the scope like this scope.$parent
because you want to bind with the scope of the parent controller of the directive. If you pass only scope
angular will try to bind with its own directive.
UPDATE
I saw your question about dynamic data not updating the $scope
.
Simply initialize the variable in the controller:
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.propriedade = {};
$scope.manipulaPropriedade = function(propriedade) {
console.log(propriedade) ;
}
});
It is not good practice to create dynamic variables in $scope
. Imagine someone who goes into maintenance trying to guess where this variable comes from later in some method.
I had already made several attempts in this direction, but without success. Thanks Even, thank you very much
– Silvério Sepulveda
It turned very good, working filet, only has a small detail, it is not being validated in the form, in this case I have to compile it in the form, instead of: element.append($Compile(input)(Scope. $Parent)); ')?
– Silvério Sepulveda
You can update your plnkr with the form for me to test?
– Giovane
Look, the form only takes into account the second field, which was created manually, the first, which was created by the directive he ignores. https://plnkr.co/edit/PIZUtNE5XEnJAM7GU8r?p=preview
– Silvério Sepulveda
I tried some things here but could not put it to be validated. At another time I try to study it. Try searching for how to validate form with angular dynamic fields.
– Giovane
Show, thanks, thank you so much for your attention.
– Silvério Sepulveda
The solution was to first include then compile, swapping the selected line for the bottom two. //element.append($Compile(input)(Scope. $Parent)); var result = element.append(input); $Compile(result)(Scope. $Parent);
– Silvério Sepulveda
I tested on your plnkr, it worked cool, good!
– Giovane
Champ, good afternoon, man I’m incrementing that routine, now it’s creating the fields searching the data of a file with a JSON variable, it’s all working, but there’s one detail that’s the following: When I create the form elements, and type in any of these elements, they don’t update $Cope, but if I put a created element in the hand together and access it first works, and even the dynamically created ones start working. https://plnkr.co/edit/RbOFEJ9hCO3T47BUqFzc?p=preview Is there a light, rsss. Hug.
– Silvério Sepulveda