So, I'm fairly new to AngularJS, and I may be approaching this problem incorrectly, especially in the way I'm using directives.
I have a main app that I would like to place some common code in, like a directive to display some messages on an html page. I have a controller that makes a rest API call to validate credentials, which then assigns some data to $scope. I'd like the directive to use that scope data to display the messages in html and do some formatting. From what I've researched, doing this from the controller is not the best practice. I know I could do this directly in the controller and just put a div in the html like:
<div>{{validation.message}}</div>
The simplified version of the html is:
<html data-ng-app="myApp" data-ng-strict-di>
<body>
<div data-ng-view data-ng-controller="MainController">
<div data-ng-app="StoredAccountsModule" data-ng-controller="AccountController">
<button class="btn btn-default" data-ng-click="modalOptions.verify(newAccount)">Verify Credentials</button>
<div data-myappmessage></div>
</div>
</div>
</body>
</html>
The directive is:
angular.module("myApp").directive("myappmessage", function () {
//✅ : ✖
return {
link: function postLink(scope, element, attrs) {
element.html('<span>' + scope.validation.message + '</span>');
}
};
});
I'm sure I'm missing something on how controllers, modules, and directives all connect together.
UPDATE
Ok, so with everyone's comments, I've worked out a lot of the kinks in what I'm trying to do. Right now I am trying to initiate an account validation from a bootstrap modal using a button click that initiates an API call to return the validation information. The modal is being initiated as a service as seen here: http://weblogs.asp.net/dwahlin/building-an-angularjs-modal-service. The modal window part that matters is:
<div data-ng-controller="MainController">
<div data-simplevalidation></div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-ng-click="modalOptions.ok(newAccount)">{{modalOptions.actionButtonText}}</button>
<button class="btn btn-warning" data-ng-click="modalOptions.close('cancel')">{{modalOptions.closeButtonText}}</button>
<button data-ng-controller="MainController" class="btn btn-default" data-ng-click="verifyAccount(newAccount)">Verify Credentials</button>
</div>
And the modal is being launched from:
<div data-ng-controller="StoredAccountsIndexController" style="width:auto;text-align:center;">
<table style="border:1px solid black;margin:auto;">
<tfoot>
<tr>
<td colspan="2"><button type="button" class="btn btn-primary" data-ng-click="open()">Add New Stored Account</button></td>
</tr>
</tfoot>
</table>
</div>
The directive is now
angular.module("myApp").directive("simplevalidation", function () {
return {
template: '<button type="button" class="btn btn-default" data-ng-click=verifyAccount(newAccount)>Verify</button><span></span>',
link: function ($scope, element, attrs) {
$scope.$watchGroup(["validation.message", "validation.success"], function () {
if ($scope.validation.success != null) {
if ($scope.validation.success) {
element.children("span").html($scope.validation.message + " " + $scope.validation.symbol);
element.children("span").css("color", "green")
}
else {
element.children("span").html($scope.validation.message + " " + $scope.validation.symbol);
element.children("span").css("color", "red")
}
}
}, true);
}
};
});
Only one app is being declared on the page, myApp. I believe the issue I'm experiencing is with the scope of the modal. If I use the button placed by the directive template, everything works the way it's supposed to. If I use the button in the modal footer, the API call fires, but I never see an update on my scope. The controller that is handling that call is:
angular.module("myApp").controller("MainController", ['$scope', '$timeout', "StoredAccountsFactory", function ($scope, $timeout, StoredAccountsFactory) {
$scope.validation = {}
$scope.verifyAccount = function (account) {
$scope.validation = {};
StoredAccountsFactory.validateStoredAccount(account).success(function (data, status, headers, config) {
$scope.validation.message = "Account credentials verified";
$scope.validation.success = true;
$scope.validation.symbol = "✅"
}).error(function (data, status, headers, config) {
$scope.validation.message = data;
$scope.validation.success = false;
$scope.validation.symbol = "✖"
});
}
}]);
Any other insight as to what I'm doing wrong here?
I've also tried a directive in this format as I believe is the preferred way to handle directive scope, without any change in outcome.
angular.module("myApp").directive("simplevalidation", function () {
//add these attributes to the directive element
//data-validate="verifyAccount(newAccount)" data-validationmessage="validation.message" data-validationsuccess="validation.success"
return {
scope: {
validate: '&',
validationmessage: '=',
validationsuccess: '='
},
template: '<button type="button" data-ng-click=validate()>Verify</button><span></span>',
link: function ($scope, element, attrs) {
$scope.$watchGroup(["validationmessage", "validationsuccess"], function () {
if ($scope.validationsuccess != null) {
if ($scope.validationsuccess) {
element.children("span").html($scope.validationmessage + " " + " ✅");
element.children("span").css("color", "green");
}
else {
element.children("span").html($scope.validationmessage + " " + " ✖");
element.children("span").css("color", "red");
}
}
}, true);
}
};
});