值我可以创建多个输入值一个不错的字符串:角JS检索跨度
<input ng-model="first">
<input ng-model="second">
<span ng-model="result">{{first}} and {{second}}</span>
如何我现在可以检索JS生成的字符串?理想的情况是类似
var myResult = $scope.result
,但我觉得我失去了一些东西......
值我可以创建多个输入值一个不错的字符串:角JS检索跨度
<input ng-model="first">
<input ng-model="second">
<span ng-model="result">{{first}} and {{second}}</span>
如何我现在可以检索JS生成的字符串?理想的情况是类似
var myResult = $scope.result
,但我觉得我失去了一些东西......
我建议做一些更是这样的:
查看
<input ng-model="first">
<input ng-model="second">
<span>{{result}}</span>
控制器
$scope.$watchCollection("[first,second]", function(newVals){
$scope.result = newVals[0] + " and " + newVals[1];
});
然后您可以使用var myResult = $scope.result
。这是一个fiddle。但只是更多的上下文,ngModel
通常不用于绑定到非输入。从docs(重点煤矿):
的ngModel指令结合的输入,选择,文本区域(或定制表单控件)到使用NgModelController,其中创建和由该指令露出的范围的特性。
所以这取决于你可能想创建自己的指令最终使用情况,但你也可能要问自己,你正在尝试做的是否必要,或者是否有更好的方式去关于它。
{{first}}和{{second}}似乎只用于演示目的。你真的需要它作为模型属性吗? –