今天我有一个问题,我需要为一个输入添加一个值,其中ng-model = "element“工作,但是如果我使用ng-model = "model.element”不再适用于我,这里的代码
<div ng-app="myApp" ng-controller="myCtrl as model">
<input type="text" ng-cero="uno" ng-model="c1ero">
<input type="text" ng-cero="dos" ng-model="model.eae" >
</div>
angular
.module("myApp",[])
.controller('myCtrl', function($scope){
var model=this;
})
.directive ('ngCero', function(){
var linkFunction =function(scope, element, attrs){
element.bind("keypress", function(event) {
if(event.which === 13) {
scope.$apply(function(){
scope.$eval(attrs.ngCero, {'event': event});
scope[attrs.ngModel]="0,";
console.log(attrs);
});
event.preventDefault();
}
});
};
return{
controller: "myCtrl",
link: linkFunction
}
}) 这里的代码是:http://codepen.io/fernandooj/pen/EgmQmJ
发布于 2016-09-27 21:00:25
当scope[attrs.ngModel]="0,";指向嵌套属性(如model.eae )时,需要解析model.eae。为此目的使用角$parse服务($parse(attrs.ngModel).assign(scope, '0, ');):
.directive ('ngCero', function($parse) {
var linkFunction =function(scope, element, attrs) {
element.bind("keypress", function(event) {
if (event.which === 13) {
scope.$apply(function(){
scope.$eval(attrs.ngCero, {'event': event});
$parse(attrs.ngModel).assign(scope, '0, ');
console.log(attrs);
});
event.preventDefault();
}
});
};
return {
controller: "myCtrl",
link: linkFunction
}
}) https://stackoverflow.com/questions/39733132
复制相似问题