'use strict';
angular.module('$praveen.directives').directive('pvTempUrl',
function ($http, $compile, $log) {
$log.info("Directive Called");
return {
restrict: 'A',
replace:true,
compile: function (telement, tattr, transclude) {
var templateloader = $http.get('../../HelloTemp.html').
success(function (data) {
$log.info("Success-" + data);
telement.html(data);
}).
error(function (data, status) {
$log.warn("Error occured - " + data + " status-" + status);
});
return function (scope, element, attr, controller) {
$log.info("Reached till return part");
templateloader.then(function () {
var compiledHtm = $compile(telement.html())(scope).html();
element.html(compiledHtm);
});
}
}
};
});
var compiledHtm = $compile(telement.html()(scope));
コードをコンパイルする代わりに、テンプレートの URL を直接使用できますか。
編集:$compile(telement.html())(scope).html();
コンパイル後にhtmlを取得するように編集しまし<input class="ng-pristine ng-valid" type="text" ng-model="txtData">{{ txtData }}
たが、それでもng-modelは機能せず、{{ txtData }]を表示しているため、コンソールにもエラーはありません。
解決済み
コンパイルされたオブジェクトではなくhtmlをバインドしていた問題が見つかりました
// Code goes here
var mymodule = angular.module('myapp', []);
mymodule.controller('mycontroller', function ($scope) {
});
mymodule.directive('pvTempUrl',
function ($http, $compile, $log, $templateCache) {
$log.info("Directive Called");
return {
restrict: 'A',
replace: true,
compile: function (telement, tattr, transclude) {
var templateloader = $http.get(tattr.pvTempUrl, { cache: $templateCache }).
success(function (data) {
$log.info("Success-" + data);
telement.html(data);
}).
error(function (data, status) {
$log.warn("Error occured - " + data + " status-" + status);
});
return function (scope, element, attr) {
templateloader.then(function () {
var compiledHtm = ($compile(telement.html())(scope));
$log.info("compiled html-" + compiledHtm);
//element.html(compiledHtm);
element.replaceWith(compiledHtm);
$log.info(element.html());
});
};
}
};
});