I want to build a angular.js directive, which by clicking the <span>, it will turn out into a editable input. and the following code works well, except when the model is empty or model length has 0, make it show <span> EMPTY </span>.
<span ng-editable="updateAccountProfile({'location':profile.location})" ng-editable-model="profile.location"></span>
app.directive('ngEditable', function() {
return {
template: '<span class="editable-wrapper">' + '<span data-ng-hide="edit" data-ng-click="edit=true;value=model;">{{model}}</span>' + '<input type="text" data-ng-model="value" data-ng-blur="edit = false; model = value" data-ng-show="edit" data-ng-enter="model=value;edit=false;"/>' + '</span>',
scope: {
model: '=ngEditableModel',
update: '&ngEditable'
},
replace: true,
link: function(scope, element, attrs) {
var value = scope.$eval(attrs.ngEditableModel);
console.log('value ' , attrs.ngEditableModel , value);
if (value == undefined || (value != undefined && value.length == 0)) {
console.log('none');
var placeHolder = $("<span>");
placeHolder.html("None");
placeHolder.addClass("label");
$(element).attr("title", "Empty value. Click to edit.");
}
scope.focus = function() {
element.find("input").focus();
};
scope.$watch('edit', function(isEditable) {
if (isEditable === false) {
scope.update();
} else {
// scope.focus();
}
});
}
};
});
the problem occurs at the this part of code that
var value = scope.$eval(attrs.ngEditableModel);
console.log('value ' , attrs.ngEditableModel , value);
attrs.ngEditableModel output the content 'profile.location', but then using scope.$eval() only output ' undefined ', even model 'profile.location' is not null
scope.$evalon an attribute which already assigned its value to your scope'smodelvariable?