我创建了一个指令。
HTML:
<ul class="leveringsdag" style="margin-top:20px;" demo-directive ></ul>
<!-- here I want to pass dat as argument probably like
<ul class="leveringsdag" style="margin-top:20px;" demo-directive data="myData"></ul>
-->
指令:
phonecatApp.directive('demoDirective', function($compile) {
return {
template: '<div></div>',
replace: true,
link: function(scope, element) {
var el = angular.element('<span/>');
// Here I want to get json passed as arguments for logic
var day = ["zondag","maandag" ,"dinsdag", "woensdag" ,"donderdag" , "vrijdag" , "zaterdag"];
var d = new Date();
var n = d.getDay();
var temp = "";
for(i=n ; i<6 ; i++){
var myString = '<li> <input class="checkleveringsdag" name="leveringsdag" value="2014-10-14" checked="" type="radio"> ' + day[i] + '</li>';
temp += myString;
}
for(i=0 ; i<n ; i++){
var myString = '<li> <input class="checkleveringsdag" name="leveringsdag" value="2014-10-14" checked="" type="radio"> ' + day[i] + '</li>';
temp += myString;
}
$compile(temp)(scope);
element.append(temp);
}
}
});
我希望从控制器获取我的指令中的数据
控制器:
$scope.pickAdelivery = function ( vendor_id ){
var data = $http.get(_BaseURL ,{headers: {'Accept': 'application/json',
'Content-Type': 'application/json' }}).then(function (result) {
// console.log(JSON.stringify(result));
console.log("Success in Wishlist AJAX");
console.log(JSON.stringify( result ));
$scope.myData = result.data; // this data i waana pass to directive
}, function (result) {
hideloader();
// commonAlert("Error:Please Try again");
console.log(JSON.stringify(result));
})
我想获取指令中的数据来执行我的逻辑。我的数据来自函数调用后的ajax。但我无法得到那个
答案 0 :(得分:0)
总结我的thoughts指令示例:
.directive('demodirective', function() {
return {
restrict: 'A',
scope:false,// this mean that directive use parent scope!!!!
link: function (scope, element, attrs) {
//get from attribute prop name
var scopePropToWatch=attrs.mymodel;
//start watching
scope.$watch(scopePropToWatch,function(newVal,oldval){
if (newVal!=''&&newVal!=null)
{
$('.insertAfterThis').append('<span>'+newVal+'</span>');
}
});
},
template: '<div class="insertAfterThis">Name: <input type="text" /> Address: some adrresss</div>'
};
});
此处正在工作fiddle