模型$ modelValue是楠指令 [英] model.$modelValue is NaN in directive

查看:161
本文介绍了模型$ modelValue是楠指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到这个的jsfiddle: http://jsfiddle.net/8bENp/66/

See this jsfiddle: http://jsfiddle.net/8bENp/66/

如果你看一下JavaScript控制台,你会看到这样的事情:

If you look at the JavaScript console, you'll see something like this:

TypeError: Object NaN has no method 'replace'
    at makeHtml (https://raw.github.com/coreyti/showdown/master/compressed/showdown.js:62:705)
    at render (http://fiddle.jshell.net/_display/:50:42)
    at link (http://fiddle.jshell.net/_display/:54:13)
    at k (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:42:321)
    at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:38:198)
    at k (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:42:261)
    at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:38:198)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:37:332
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:15:440
    at Object.e.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:85:416) <markdown ng-model="someCode" class="ng-pristine ng-valid"> angular.min.js:60

问题是,模式。$ modelValue NaN的时,它的类型甚至不应该是一个数字。尽管如此,降价呈现。我可以添加一个 typeof运算模式。$ modelValue =='字符串'检查,但我宁愿治疗的根本原因。任何想法?

The problem is that model.$modelValue is NaN when its type shouldn't even be a number. Nevertheless, the markdown renders. I could add a typeof model.$modelValue == 'string' check, but I'd rather treat the underlying cause. Any idea?

推荐答案

在你的指令中的问题是,前pression已评估前角触发手表一次。所以第一次的值未定义。我不相信,可以pvented $ P $,但如何AngularJS的作品。

The problem in your directive is that angular triggers the watch once before the expression has been evaluated. So the very first time the value is undefined. I don't believe that can be prevented, but is how AngularJS works.

我添加了一个 VAL 参数来渲染功能来显示实际观看值(记录到控制台 - 见底部的小提琴)。在 ngModelController 初始化 $ modelValue 为NaN ,这就是为什么 NaN的传递给函数,而不是未定义

I added a val parameter to your render function to show the actual watched value (logged to the console--see the fiddle at the bottom). The ngModelController initializes $modelValue to NaN, that's why NaN is passed to the function instead of undefined.

但是,因为它好像在 makeHtml 函数需要字符串,一个简单的办法是将它传递一个空字符串,如果该值为falsy(可能还要好其转换为字符串)。

But since it seems as if the makeHtml function expects a string, an easy fix is to pass it an empty string if the value is falsy (might be even better to convert it to a string).

var htmlText = converter.makeHtml(model.$modelValue || '');

更新拨弄

这篇关于模型$ modelValue是楠指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆