如何美元角JS p $ pvent属性更改 [英] How to prevent property change on Angular JS
问题描述
我使用我的项目AngularJs,我已经连接到一个下拉我的ViewModel财产(小于选择>)
这有下拉一个空值女巫被默认选中,我想要的是prevent用户选择空值后,他选择一些其他的价值。
香港专业教育学院开始看$看,但我不知道是否有某种方式来取消转变OOF的财产,有些事情是这样的:
$范围。$腕表('myProp',函数(的newval,OLDVAL,范围){
如果(的newval){范围preventDefault()。 }
}
任何想法,这是基本的想法,在更先进的开发我要问用户进行确认。
什么想法?
我想要的是prevent用户选择空值后,他选择一些其他的价值
块引用>这应该自动为您只要你不最初分配NG-模型属性的值发生。因此,使用
<选择>
如下图所示,在控制器不初始化$ scope.selected_year:<选择NG模型=selected_yearNG选项=一年一年年>< /选择>
在列表中显示最初,角将增加这样对HTML的一个选项,因为$ scope.selected_year当前未设置为一个有效的选项/值:
<?期权价值=选择=选择>< /选项>
选择一个有效的选择后,该选项会奇迹般地消失,因此用户将无法再选择它。尝试在此小提琴。
如果在NG-模型属性已经在第一次显示选择列表中指定一个有效的值,那么你可以指定一个控制器功能的<一个href=\"http://stackoverflow.com/questions/14700325/what-are-the-undocumented-features-of-angular\">undocumented NG-改变参数:
&LT;选择... ng的变化=preventUserFromDoingXzy()&GT;
在功能preventUserFromDoingXzy(),你可以做你需要做的来控制,用户可以选择或修改模型的东西。
I'm using AngularJs on my project and i've a property on my viewModel that is connected to a dropdown (< select >)
that dropdown have a empty value witch is selected by default, what i want is to prevent user to select that empty value after he select some other value.
ive started to look to $watch, but i dont know if there is some way to cancel the "changing oof that property", some thing like this:
$scope.$watch('myProp', function (newVal, oldVal, scope) { if (newVal) { scope.preventDefault(); } }
any idea, this is the base idea, on a more advanced development i need to ask users for a confirmation.
any ideas?
解决方案what i want is to prevent user to select that empty value after he select some other value
This should happen automatically for you, as long as you don't assign the ng-model property a value initially. So using the
<select>
shown below, don't initialize $scope.selected_year in your controller:<select ng-model="selected_year" ng-options="year for year in years"></select>
When the list displays initially, Angular will have added an option like this to the HTML, since $scope.selected_year is not currently set to a valid option/value:
<option value="?" selected="selected"></option>
After selecting a valid choice, that option will magically disappear, so the user will not be able to select it again. Try it in this fiddle.
If the ng-model property already has a valid value assigned when the select list is first displayed, then you can assign a controller function to the undocumented ng-change parameter:
<select ... ng-change="preventUserFromDoingXzy()">
Inside function preventUserFromDoingXzy() you can do what you need to do to control what the user can select, or modify the model.
这篇关于如何美元角JS p $ pvent属性更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!