【发布时间】:2019-03-21 13:51:34
【问题描述】:
我是使用 angularjs 的新手,我正在尝试创建指令。我的查询是,如何从 html 更改 $http.get 的 URL。这是我的代码:
HTML 指令:
<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>
谢谢!
【问题讨论】:
-
您的意思是从 JavaScript 更改 $http.get 调用,而不是从 HTML 调用?
-
重新分解应用程序以让父控制器从服务器获取数据会更明智。避免表单组件中的异步操作,因为这会使测试和调试变得困难。
标签: javascript angularjs angularjs-directive angularjs-scope angularjs-templates