从指令绑定到ng-model以访问$ dirty和$ valid

时间:2015-09-23 01:46:13

标签: javascript angularjs

我只想知道是否有办法绑定到formName.firstName。$ dirty和formName.firstName。$ valid在指令内。我写了一个使用标准角度绑定。这是我的代码:

<form action="#" name="formName>
    <feedback model="firstName">
        <input type="text" name="model" class="form-control" ng-pattern="/\b[A-Za-z|\'|\s]{2,}\b/" ng-model="model"
               ng-model-options="{ updateOn: 'default blur change', debounce: 500 }" required=""
               placeholder="enter your first name">
    </feedback>
</form>

我想采取跨度并将它们变成指令,以便我可以重复使用它。所以我的代码看起来像这样。 (我想使用的代码,不是真正的代码

D/CrashAnrDetector( 2960): Build: samsung/chagallltedo/chagalllte:5.0.2/LRX22G/T805YDOU1BOG1:user/release-keys
D/CrashAnrDetector( 2960): Hardware: universal5420 D/CrashAnrDetector( 2960): Revision: 7 
D/CrashAnrDetector( 2960): Bootloader: T805YDOU1BOG1 D/CrashAnrDetector( 2960): Radio: unknown 
D/CrashAnrDetector( 2960): Kernel: Linux version 3.4.39-5331373 (dpi@SWDD6318) (gcc version 4.8 (GCC) ) #1 SMP PREEMPT Mon Jul 6 17:07:24 KST 2015 
D/CrashAnrDetector( 2960): D/CrashAnrDetector( 2960): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** D/CrashAnrDetector( 2960): Build fingerprint: 'samsung/chagallltedo/chagalllte:5.0.2/LRX22G/T805YDOU1BOG1:user/release-keys' 
D/CrashAnrDetector( 2960): Revision: '7' D/CrashAnrDetector( 2960): ABI: 'arm' 
D/CrashAnrDetector( 2960): pid: 20024, tid: 20073, name: RenderThread  >>> com.example.test <<< 
D/CrashAnrDetector( 2960): signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr 
D/CrashAnrDetector( 2960): processName:com.example.test 
D/CrashAnrDetector( 2960): broadcastEvent : com.example.test SYSTEM_TOMBSTONE 
W/ContextImpl( 2960): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1714 
com.android.server.analytics.data.collection.application.CrashAnrDetector.broadcastEvent:296 
com.android.server.analytics.data.collection.application.CrashAnrDetector.processDropBoxEntry:254 
com.android.server.analytics.data.collection.application.CrashAnrDetector.access$100:60 
com.android.server.analytics.data.collection.application.CrashAnrDetector$1.onReceive:102 
D/PhoneWindow( 2960): *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null 
D/PhoneWindow( 2960): *FMB* isFloatingMenuEnabled return false 
I/SurfaceFlinger( 2313): id=1402 createSurf (97x97),1 flag=4, nidig I/OpenGLRenderer( 2960): Initialized EGL, version 1.4 I/OpenGLRenderer( 2960): HWUI protection enabled for context ,  &this =0x9e13c268 ,&mEglDisplay = 1 , &mEglConfig = -1709931596 E/android.os.Debug( 2960): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error 
I/dumpstate(21554): begin I/dumpstate(21554): dumpstate is still running 
D/SensorService( 2960): [SO] 0.450 0.153 9.874 W/ActivityManager( 2960): Activity pause timeout for ActivityRecord{247b8582 u0 
com.example.test/.TelerikCallbackActivity t418 f} 
D/CustomFrequencyManagerService( 2960): acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1800000  uid : 1000  pid : 2960  pkgName : ACTIVITY_RESUME_BOOSTER@2 
W/ActivityManager( 2960): mDVFSHelper.acquire()

是否可以在指令中绑定到$ valid,$ invalid和$ dirty,并根据表单模型触发它。我不确定我是否正确地提出这个问题。我希望代码能解释我的意图。

如果有必要,我可以发布我写的东西,或者在jsbin或plunkr上贴一些东西。我见过类似的代码,人们使用element.bind()模拟了ng-model的代码,我希望有一种更有棱角的方法。

1 个答案:

答案 0 :(得分:1)

您可以将属性绑定到指令范围。 喜欢这个

&#13;
&#13;
app.directive('feedback', [function(version) {
    return {
        restrict: 'E',
        scope: {
            dirty: '=dirty'
        },
        link: function(scope, elem, ctrl) {
            scope.$watch('dirty', function(newValue, oldValue) {
                console.log('form:', newValue);
            });
        }
    }
}]);
&#13;
<feedback dirty="formName.firstName.$dirty"></feedback>
&#13;
&#13;
&#13;