Load template attribute in AngularJS directive for $http.getHow does data binding work in AngularJS?'this' vs $scope in AngularJS controllersAngularJS : Prevent error $digest already in progress when calling $scope.$apply()How do I access the $scope variable in browser's console using AngularJS?AngularJS passing data to $http.get requestWhat is the difference between '@' and '=' in directive scope in AngularJS?“Thinking in AngularJS” if I have a jQuery background?How do I use $scope.$watch and $scope.$apply in AngularJS?AngularJS: Service vs provider vs factoryif else statement in AngularJS templates

Drawing a topological "handle" with Tikz

Can I use my Chinese passport to enter China after I acquired another citizenship?

Open a doc from terminal, but not by its name

Why did the EU agree to delay the Brexit deadline?

Count the occurrence of each unique word in the file

Can someone explain how this makes sense electrically?

Why did the HMS Bounty go back to a time when whales are already rare?

On a tidally locked planet, would time be quantized?

What major Native American tribes were around Santa Fe during the late 1850s?

Longest common substring in linear time

Gibbs free energy in standard state vs. equilibrium

Some numbers are more equivalent than others

How should I respond when I lied about my education and the company finds out through background check?

Why do IPv6 unique local addresses have to have a /48 prefix?

Do varchar(max), nvarchar(max) and varbinary(max) columns affect select queries?

Global amount of publications over time

Engineer refusing to file/disclose patents

Journal losing indexing services

Two-sided logarithm inequality

Query about absorption line spectra

Database accidentally deleted with a bash script

Will adding a BY-SA image to a blog post make the entire post BY-SA?

THT: What is a squared annular “ring”?

Do the concepts of IP address and network interface not belong to the same layer?



Load template attribute in AngularJS directive for $http.get


How does data binding work in AngularJS?'this' vs $scope in AngularJS controllersAngularJS : Prevent error $digest already in progress when calling $scope.$apply()How do I access the $scope variable in browser's console using AngularJS?AngularJS passing data to $http.get requestWhat is the difference between '@' and '=' in directive scope in AngularJS?“Thinking in AngularJS” if I have a jQuery background?How do I use $scope.$watch and $scope.$apply in AngularJS?AngularJS: Service vs provider vs factoryif else statement in AngularJS templates













1















I am new using angularjs and I am trying to create directives. My query is, how can I change the URL of the $http.get from the html. This is my code:



HTML directive:



<form-directive text="Formulario con Directiva" nameinput="true"
namelabel="Nombre:" emailinput="true"
emaillabel="Email:" subjetinput="true" subjetlabel="Asunto:"
message="true" messagelabel="Mensaje:"
dataurl="URL to change">
</form-directive>


JS:



<script>
angular.module('testDirective', [])
.controller('testDir', function ($scope, $http)
$scope.textoFalopa = "Hola, es una prueba";
)
.directive('formDirective', function ()
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($scope, $http)
$http.get('https://jsonplaceholder.typicode.com/users').then(function (remotedata)
console.log(remotedata.data);
$scope.data = remotedata.data;
);
],
link: function (scope)
console.log(scope);

;
);

</script>


Thank you!










share|improve this question









New contributor




nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • You mean change your $http.get call from JavaScript, not from HTML right?

    – ControlAltDel
    Mar 21 at 13:54











  • It would be wiser to re-factor the app to have the parent controller fetch the data from the server. Avoid asynchronous operations in the form component as it makes testing and debugging difficult.

    – georgeawg
    Mar 21 at 14:20
















1















I am new using angularjs and I am trying to create directives. My query is, how can I change the URL of the $http.get from the html. This is my code:



HTML directive:



<form-directive text="Formulario con Directiva" nameinput="true"
namelabel="Nombre:" emailinput="true"
emaillabel="Email:" subjetinput="true" subjetlabel="Asunto:"
message="true" messagelabel="Mensaje:"
dataurl="URL to change">
</form-directive>


JS:



<script>
angular.module('testDirective', [])
.controller('testDir', function ($scope, $http)
$scope.textoFalopa = "Hola, es una prueba";
)
.directive('formDirective', function ()
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($scope, $http)
$http.get('https://jsonplaceholder.typicode.com/users').then(function (remotedata)
console.log(remotedata.data);
$scope.data = remotedata.data;
);
],
link: function (scope)
console.log(scope);

;
);

</script>


Thank you!










share|improve this question









New contributor




nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • You mean change your $http.get call from JavaScript, not from HTML right?

    – ControlAltDel
    Mar 21 at 13:54











  • It would be wiser to re-factor the app to have the parent controller fetch the data from the server. Avoid asynchronous operations in the form component as it makes testing and debugging difficult.

    – georgeawg
    Mar 21 at 14:20














1












1








1








I am new using angularjs and I am trying to create directives. My query is, how can I change the URL of the $http.get from the html. This is my code:



HTML directive:



<form-directive text="Formulario con Directiva" nameinput="true"
namelabel="Nombre:" emailinput="true"
emaillabel="Email:" subjetinput="true" subjetlabel="Asunto:"
message="true" messagelabel="Mensaje:"
dataurl="URL to change">
</form-directive>


JS:



<script>
angular.module('testDirective', [])
.controller('testDir', function ($scope, $http)
$scope.textoFalopa = "Hola, es una prueba";
)
.directive('formDirective', function ()
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($scope, $http)
$http.get('https://jsonplaceholder.typicode.com/users').then(function (remotedata)
console.log(remotedata.data);
$scope.data = remotedata.data;
);
],
link: function (scope)
console.log(scope);

;
);

</script>


Thank you!










share|improve this question









New contributor




nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am new using angularjs and I am trying to create directives. My query is, how can I change the URL of the $http.get from the html. This is my code:



HTML directive:



<form-directive text="Formulario con Directiva" nameinput="true"
namelabel="Nombre:" emailinput="true"
emaillabel="Email:" subjetinput="true" subjetlabel="Asunto:"
message="true" messagelabel="Mensaje:"
dataurl="URL to change">
</form-directive>


JS:



<script>
angular.module('testDirective', [])
.controller('testDir', function ($scope, $http)
$scope.textoFalopa = "Hola, es una prueba";
)
.directive('formDirective', function ()
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($scope, $http)
$http.get('https://jsonplaceholder.typicode.com/users').then(function (remotedata)
console.log(remotedata.data);
$scope.data = remotedata.data;
);
],
link: function (scope)
console.log(scope);

;
);

</script>


Thank you!







javascript angularjs angularjs-directive angularjs-scope angularjs-templates






share|improve this question









New contributor




nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









ChrisM

522316




522316






New contributor




nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 21 at 13:51









nicounyinicounyi

83




83




New contributor




nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






nicounyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • You mean change your $http.get call from JavaScript, not from HTML right?

    – ControlAltDel
    Mar 21 at 13:54











  • It would be wiser to re-factor the app to have the parent controller fetch the data from the server. Avoid asynchronous operations in the form component as it makes testing and debugging difficult.

    – georgeawg
    Mar 21 at 14:20


















  • You mean change your $http.get call from JavaScript, not from HTML right?

    – ControlAltDel
    Mar 21 at 13:54











  • It would be wiser to re-factor the app to have the parent controller fetch the data from the server. Avoid asynchronous operations in the form component as it makes testing and debugging difficult.

    – georgeawg
    Mar 21 at 14:20

















You mean change your $http.get call from JavaScript, not from HTML right?

– ControlAltDel
Mar 21 at 13:54





You mean change your $http.get call from JavaScript, not from HTML right?

– ControlAltDel
Mar 21 at 13:54













It would be wiser to re-factor the app to have the parent controller fetch the data from the server. Avoid asynchronous operations in the form component as it makes testing and debugging difficult.

– georgeawg
Mar 21 at 14:20






It would be wiser to re-factor the app to have the parent controller fetch the data from the server. Avoid asynchronous operations in the form component as it makes testing and debugging difficult.

– georgeawg
Mar 21 at 14:20













1 Answer
1






active

oldest

votes


















0














I'm assuming what you are looking to do is to get the value of the attribute on your directive declaration dataurl="URL to change" and use that as the URL in the $http call.



The properties in the scope object define the bindings of these attributes to your AngularJS scope (injected as $scope).



You have already bound dataurl to your scope, so you're half way there. Now the the most straightforward way of getting the in the example you have posted would be to just use the $scope object in your controller. Like so:



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($element, $http)
$http.get($scope.dataurl).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Be advised that best practice with AngularJS is now to only use $scope directly when needing it's advanced features. For this kind of simple case you shouldn't need to inject it. I would suggest looking into AngularJS components and/or the bindToController property.



An alternative (but perhaps messy) solution if you just want to get the attribute is to inject $element which gives you access to the underlying jQuery object.



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
controller: ['$element', '$http', function ($scope, $http)
$http.get($element.attr('dataurl')).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Though this isn't really the 'angular way' so I'd only use it for quick hacks or messy workarounds.



So you have three approaches there. Any one will work, but I would suggest learning components and controller binding as that will encourage good practice and put you in good stead for learning Angular 2+






share|improve this answer


















  • 1





    Thank you very much! it worked perfectly

    – nicounyi
    Mar 21 at 15:07










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






nicounyi is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281981%2fload-template-attribute-in-angularjs-directive-for-http-get%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I'm assuming what you are looking to do is to get the value of the attribute on your directive declaration dataurl="URL to change" and use that as the URL in the $http call.



The properties in the scope object define the bindings of these attributes to your AngularJS scope (injected as $scope).



You have already bound dataurl to your scope, so you're half way there. Now the the most straightforward way of getting the in the example you have posted would be to just use the $scope object in your controller. Like so:



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($element, $http)
$http.get($scope.dataurl).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Be advised that best practice with AngularJS is now to only use $scope directly when needing it's advanced features. For this kind of simple case you shouldn't need to inject it. I would suggest looking into AngularJS components and/or the bindToController property.



An alternative (but perhaps messy) solution if you just want to get the attribute is to inject $element which gives you access to the underlying jQuery object.



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
controller: ['$element', '$http', function ($scope, $http)
$http.get($element.attr('dataurl')).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Though this isn't really the 'angular way' so I'd only use it for quick hacks or messy workarounds.



So you have three approaches there. Any one will work, but I would suggest learning components and controller binding as that will encourage good practice and put you in good stead for learning Angular 2+






share|improve this answer


















  • 1





    Thank you very much! it worked perfectly

    – nicounyi
    Mar 21 at 15:07















0














I'm assuming what you are looking to do is to get the value of the attribute on your directive declaration dataurl="URL to change" and use that as the URL in the $http call.



The properties in the scope object define the bindings of these attributes to your AngularJS scope (injected as $scope).



You have already bound dataurl to your scope, so you're half way there. Now the the most straightforward way of getting the in the example you have posted would be to just use the $scope object in your controller. Like so:



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($element, $http)
$http.get($scope.dataurl).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Be advised that best practice with AngularJS is now to only use $scope directly when needing it's advanced features. For this kind of simple case you shouldn't need to inject it. I would suggest looking into AngularJS components and/or the bindToController property.



An alternative (but perhaps messy) solution if you just want to get the attribute is to inject $element which gives you access to the underlying jQuery object.



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
controller: ['$element', '$http', function ($scope, $http)
$http.get($element.attr('dataurl')).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Though this isn't really the 'angular way' so I'd only use it for quick hacks or messy workarounds.



So you have three approaches there. Any one will work, but I would suggest learning components and controller binding as that will encourage good practice and put you in good stead for learning Angular 2+






share|improve this answer


















  • 1





    Thank you very much! it worked perfectly

    – nicounyi
    Mar 21 at 15:07













0












0








0







I'm assuming what you are looking to do is to get the value of the attribute on your directive declaration dataurl="URL to change" and use that as the URL in the $http call.



The properties in the scope object define the bindings of these attributes to your AngularJS scope (injected as $scope).



You have already bound dataurl to your scope, so you're half way there. Now the the most straightforward way of getting the in the example you have posted would be to just use the $scope object in your controller. Like so:



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($element, $http)
$http.get($scope.dataurl).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Be advised that best practice with AngularJS is now to only use $scope directly when needing it's advanced features. For this kind of simple case you shouldn't need to inject it. I would suggest looking into AngularJS components and/or the bindToController property.



An alternative (but perhaps messy) solution if you just want to get the attribute is to inject $element which gives you access to the underlying jQuery object.



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
controller: ['$element', '$http', function ($scope, $http)
$http.get($element.attr('dataurl')).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Though this isn't really the 'angular way' so I'd only use it for quick hacks or messy workarounds.



So you have three approaches there. Any one will work, but I would suggest learning components and controller binding as that will encourage good practice and put you in good stead for learning Angular 2+






share|improve this answer













I'm assuming what you are looking to do is to get the value of the attribute on your directive declaration dataurl="URL to change" and use that as the URL in the $http call.



The properties in the scope object define the bindings of these attributes to your AngularJS scope (injected as $scope).



You have already bound dataurl to your scope, so you're half way there. Now the the most straightforward way of getting the in the example you have posted would be to just use the $scope object in your controller. Like so:



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
scope:
text: '@',
nameinput: '=nameinput',
namelabel: '@',
emailinput: '=emailinput',
emaillabel: '@',
subjetinput: '=subjetinput',
subjetlabel: '@',
message: '=message',
messagelabel: '@',
dataurl:'='
,
controller: ['$scope', '$http', function ($element, $http)
$http.get($scope.dataurl).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Be advised that best practice with AngularJS is now to only use $scope directly when needing it's advanced features. For this kind of simple case you shouldn't need to inject it. I would suggest looking into AngularJS components and/or the bindToController property.



An alternative (but perhaps messy) solution if you just want to get the attribute is to inject $element which gives you access to the underlying jQuery object.



angular.module('testDirective').directive('formDirective', function () 
return
restrict: "EA",
templateUrl: './template.html',
controller: ['$element', '$http', function ($scope, $http)
$http.get($element.attr('dataurl')).then(function (remotedata)
console.log(remotedata.data);
);
]
;
);


Though this isn't really the 'angular way' so I'd only use it for quick hacks or messy workarounds.



So you have three approaches there. Any one will work, but I would suggest learning components and controller binding as that will encourage good practice and put you in good stead for learning Angular 2+







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 14:25









ChrisMChrisM

522316




522316







  • 1





    Thank you very much! it worked perfectly

    – nicounyi
    Mar 21 at 15:07












  • 1





    Thank you very much! it worked perfectly

    – nicounyi
    Mar 21 at 15:07







1




1





Thank you very much! it worked perfectly

– nicounyi
Mar 21 at 15:07





Thank you very much! it worked perfectly

– nicounyi
Mar 21 at 15:07












nicounyi is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















nicounyi is a new contributor. Be nice, and check out our Code of Conduct.












nicounyi is a new contributor. Be nice, and check out our Code of Conduct.











nicounyi is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281981%2fload-template-attribute-in-angularjs-directive-for-http-get%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해