如何在javascript中填充Angularjs $ scope变量? [英] How to populate Angularjs $scope variable in javascript?

查看:87
本文介绍了如何在javascript中填充Angularjs $ scope变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mapApp.controller("myController", function ($scope,$http) {
            $scope.namePlaceHolder= "Name";
            $scope.name = "";
};

我将范围变量绑定到html输入,如下所示.

I bound a scope variable to an html input as follows.

<input id="foo" type="text" placeholder="{{namePlaceHolder}}" ng-model="name" value="{{name}}"/>

如果用户在文本框中键入内容,则$ scope.name属性将更改.但是当我使用javascript更改它时,$ scope.name数据不会更改.

If a user types something in text box the $scope.name property changes. But when I change it using javascript the $scope.name data doesn't change.

on(document.getElementById("button"), "click", function (e) {
        document.getElementById("foo").value = "ascd...";
})

此代码不会填充$ scope.name数据.

This code does not populate $scope.name data.

推荐答案

从外部元素访问范围:

on(document.getElementById("button"), "click", function (e) {
            var scope = angular.element(document.getElementById("foo")).scope();
            scope.name = "hello, World!";
    })

这篇关于如何在javascript中填充Angularjs $ scope变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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