Trying to initialize input fields in a directive with server data, but facing issues with ng-model. Previously used $scope.field1 = $scope.first.field1 in the controller before using directive.
Code simplified for readability:
In controller:
app.controller('MyController',
['$scope', 'myData', function($scope, myData) {
myData.then(function(data) {
$scope.first = data.first;
$scope.second = data.second;
});
}]);
First and second fields: field1 and field2.
In HTML code:
<h1>First</h1>
<my-directive info="first"></my-directive>
<h1>Second</h1>
<my-directive info="second"></my-directive>
Directive:
app.directive('myDirective', function() {
return {
restrict: 'E',
scope: {
info: '='
},
templateUrl: 'static/js/myDirective.html',
link: function(scope, element, attrs) {
scope.doStuff = function() {
/* Do stuff with
scope.field1 and scope.field2 */
}
}
};
});
myDirective.html:
<input type="text" ng-model="myfield1" />
<input type="text" ng-model="myfield2" />
<input type="submit" ng-click="doStuff()" />
If using:
<input type="text" value="info.field1" />
The value shows up successfully.
Looking for ideas to resolve the issue.