jsonファイルからhtmlをロードする必要があります。だから私はdformがそのための最良の解決策であることを発見した。それはjQueryプラグインなので、私はAngularでそれを必要とします。私はそれがjsfiddleの指示であることがわかったのは幸いです。jQuery関数がディレクティブに変換されていません。
唯一の違いは、私自身のjsonファイルがあり、コードを別に書いたことです。私は、HTTPリクエストを介してjsonファイルからデータを取得するためにサービスを使用しています。 その後、私はjsfiddleのような指令を使用しています。
app.js
var app = angular.module('angularApp', []);
service.js
app.service('loaderService', ['$http', function ($http) {
this.getLoadedHtml = function (callback)
{
return $http.get('../source/file.json')
.then(function (res) {
callback(res.data);
});
}
}])
controller.js
app.controller('mainCtrl', ['$scope', 'loaderService', function ($scope, loaderService) {
loaderService.getLoadedHtml(function (data) {
$scope.fields = data;
});
}]);
directive.js
app.directive('dform', function() {
return {
scope: {
action: '@',
method: '@',
html: '='
},
link: function (scope, elm, attrs) {
var config = {
"action": scope.action,
"method": scope.method,
"html": scope.html
};
elm.dform(config);
}
};
})
index.htmlを
<div class="container-fluid" ng-app="angularApp">
<div ng-controller="mainCtrl" style="background-color:green; margin-top:100px; height:1000px;">
<div class="col-sm-3" id="source-area">
<div action="index.html" method="get" html="fields" dform></div>
</div>
</div>
</div>
file.json
[
{
"type": "p",
"html": "You must login"
},
{
"name": "username",
"id": "txt-username",
"caption": "Username",
"type": "text",
"placeholder": "E.g. [email protected]"
},
{
"name": "password",
"caption": "Password",
"type": "password"
},
{
"type": "submit",
"value": "Login"
}
]
私もjQueryとDFORMライブラリを使用していました。
私は何も間違いはありません。私の配列はコントローラに正常に入ってきており、$ scope.fieldsもコントローラで正常に動作しています。私はレンダリングされたhtmlを見ることができませんか?私はjsfiddleと同じものを使っています。
jqueryプラグインを使用するアプローチは意味がありません。 jsonはどのような外観をしていますか、そしてそれからexpecpedされた結果は何ですか?これは、角度テンプレートとデータマッピングを使用して簡単に達成する必要があります – charlietfl
@charlietflなぜjsfiddleで動作していますか?それは私のコードで動いているのですか? – Umar
しかし、なぜあなたは最初にこのようにしていますか?強くあなたがhttp://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-backgroundを読むことをお勧めします – charlietfl