I'm fairly new to AngularJS Directives and was wondering how I could implement passing a custom string, which is a url that contains a controller variable, to be used later on by that controller?
Calling Directive
<div ng-controller="MyController">
<my-custom-form after-submit="group/{{insertedId}}" />
</div>
Directive
app.directive('myCustomForm', function() {
return {
templateUrl: 'myCustomForm.html',
link: function(scope, element, attr) {
scope.loadUrl = attr.afterSubmit;
}
}
});
Controller
app.controller('MyController', function($scope, $location, $http) {
$scope.loadUrl = '';
$scope.insertedId = NULL;
$http({
method:'POST',
url:'edit.php',
data:formData
}).success(function(response) {
$scope.insertedId = response.dbInsertedPrimaryId; // used in "after-submit"
$location.path($scope.loadUrl);
});
});
So when $location.path() called it sends me to a url for example:
http://example.com/group/22
The reason for this is so that I can pass a specific/different url each time the directive is used, like so
<div ng-controller="MyController">
<my-custom-form after-submit="user/{{insertedId}}" />
<my-custom-form after-submit="home/{{insertedId}}/something" />
<my-custom-form after-submit="{{insertedId}}/anything" />
</div>
Or maybe there is a better way to do this? I don't know that's why I'm asking. Thanks.