2014-01-22 165 views
2

我希望當我點擊更新按鈕時,它應該驗證用戶爲必填字段,我添加屬性電子需求但無法正常工作。必填字段驗證

請參閱此琴: http://jsfiddle.net/NfPcH/669/

HTML:

<h4>Angular-xeditable Trigger manually (Bootstrap 3)</h4> 
<div ng-app="app" ng-controller="Ctrl"> 
<span editable-text="user.name" e-form="textBtnForm" buttons="no" e-required /> 
{{ user.name || 'empty' }} 
</span> 
<button class="btn btn-default" ng-click="(textBtnForm.$visible && save()) || textBtnForm.$show()" > 
    {{textBtnForm.$visible | editOrUpdate}} 
</button> 
</div> 

的js控制器:

var app = angular.module("app", ["xeditable"]); 

app.filter('editOrUpdate', function() { 
return function(arg) { 
    return (arg) ? 'Update' : 'Edit'; 
}; 
}); 

app.run(function(editableOptions) { 
editableOptions.theme = 'bs3'; 
}); 

app.controller('Ctrl', function($scope) { 
$scope.user = { 
name: 'awesome user' 
}; 
$scope.save = function(event){ 
    $scope.textBtnForm.$save(); 
    //save here your data 
    //call the object that you passed ($scope.user) 
    //get te specific value at ($scope.user.name) 
    //alert($scope.user.name); 
    return true; 
}; 
}); 

請幫我解決這個問題。

謝謝。

+0

什麼是$ scope.textBtnForm $保存(); ngForm指令沒有$ save方法http://docs.angularjs.org/api/ng.directive:form.FormController – Whisher

回答

1

請嘗試如下所示。祝好運! :)

的Html

<div ng-app="app" ng-controller="Ctrl"> 
    <span editable-text="user.name" e-form="textBtnForm" e-required> 
    {{ user.name || 'empty' }} 
    </span> 
    <button class="btn btn-default" ng-click="textBtnForm.$show()" ng-hide="textBtnForm.$visible"> 
    edit 
    </button> 
</div> 

JS

var app = angular.module("app", ["xeditable"]); 

app.run(function(editableOptions) { 
    editableOptions.theme = 'bs3'; 
}); 

app.controller('Ctrl', function($scope) { 
    $scope.user = { 
    name: 'awesome user' 
    }; 
}); 

現場演示:JSFiddle