【发布时间】:2016-03-11 18:48:14
【问题描述】:
我想在我的模板中将 ng-model(degree) 值更改为 id(degree_id),如下所示
<md-select flex class="md-select-form" ng-model="education.degree" placeholder="Degree" save-id required-param="{{education.degree_id}}">
<md-option ng-value="degree" ng-repeat="degree in ['High School', 'Associates Degree', 'Bachelor Degree', 'Masters Degree', 'M.B.A', 'M.D', 'Ph.D', 'other']">{{degree}}</md-option>
</md-select>
我已经在我的控制器中使用了指令(save-id)进行解析,并且我已经提取了学位服务数据并绑定了相关的 id。代码如下
.directive('saveId', function(Profile){
return {
require: 'ngModel',
scope: {
requiredParam:'@'
},
link: function(scope, element, attrs, ngModel) {
console.log("initial loading");
// view --> model (change to string)
ngModel.$parsers.push(function(viewValue){
var id = -1;
var keepGoing = true;
Profile.getDegreeList().then(function(data) {
angular.forEach(data, function(ob){
if(keepGoing) {
if(ob.degree == viewValue){
id = ob.degree_id;
keepGoing = false;
}
}
});
//console.log(id); // it gives updated value
});
console.log(id); // it gives -1
return id;
});
}
};
})
我已经更新了上面提到的模型值问题是,只有更新的值在里面可用
Profile.getDegreeList(){ }
这个函数的值为 -1 ,这里有什么问题? 请给我解决办法??
【问题讨论】:
-
你到底想做什么?我认为您的指令会在
link块中返回某些内容,这很奇怪。 -
您能否提供您想要实现的目标的 Plunk(或其他来源)?很难掌握你想要做什么。
-
考虑一下这个问题:plnkr.co/edit/oDHyEp4YNlgEGpdZrsvp?p=preview 你还需要什么?
-
实际上我需要将该值更改为 id bro
-
你指的是哪个值?是否可以分叉该 Plunk 并添加到它以反映您的需要(不必工作)?
标签: angularjs angularjs-directive angularjs-scope angularjs-service angularjs-ng-model