相关疑难解决方法(0)

使用jQuery设置输入值后更新Angular模型

我有这个简单的场景:

输入元素,其值由jQuery的val()方法更改.

我试图用jQuery设置的值更新角度模型.我试着编写一个简单的指令,但它并没有按照我的意愿行事.

这是指令:

var myApp = angular.module('myApp', []);
myApp.directive('testChange', function() {
    return function(scope, element, attrs) {        
        element.bind('change', function() {
            console.log('value changed');
        })
    }
})
Run Code Online (Sandbox Code Playgroud)

这是jQuery的一部分:

$(function(){
    $('button').click(function(){
        $('input').val('xxx');
    })
})
Run Code Online (Sandbox Code Playgroud)

和HTML:

<div ng-app="myApp">
    <div ng-controller="MyCtrl">
        <input test-change ng-model="foo" />
        <span>{{foo}}</span>
    </div>
</div>

<button>clickme</button>
Run Code Online (Sandbox Code Playgroud)

这是我尝试的小提琴:http:
//jsfiddle.net/U3pVM/743/

有人可以指点我正确的方向吗?

jquery bind directive angularjs

156
推荐指数
6
解决办法
17万
查看次数

标签 统计

angularjs ×1

bind ×1

directive ×1

jquery ×1