设定范围财产NG-init没有工作 [英] Setting scope property with ng-init doesn't work

查看:79
本文介绍了设定范围财产NG-init没有工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用设置NG-init中的$ scope属性的值,我无法访问控制器的JavaScript该值。我究竟做错了什么?这里是一个小提琴: http://jsfiddle.net/uce3H/

I am trying to set the value of a $scope property using ng-init, and I am unable to access that value in the controller's javascript. What am I doing wrong? Here is a fiddle: http://jsfiddle.net/uce3H/

标记:

<body ng-app>
    <div ng-controller="testController" >
        <input type="hidden" id="testInput" ng-model="testInput" ng-init="testInput='value'" />
    </div>
    {{ testInput }}
</body>

JavaScript的:

javascript:

var testController = function ($scope) {
     console.log($scope.testInput);
}

在javascrippt,$ scope.testInput是不确定的。不应该是价值?

In the javascrippt, $scope.testInput is undefined. Shouldn't be 'value'?

推荐答案

您正在尝试角完成分配之前阅读设定值。

You are trying to read the set value before Angular is done assigning.

演示:

var testController = function ($scope, $timeout) {
    console.log('test');
    $timeout(function(){
        console.log($scope.testInput);
    },1000);
}

在理想情况下,你应该使用 $观看由@Beterraba建议摆脱定时器:

Ideally you should use $watch as suggested by @Beterraba to get rid of the timer:

var testController = function ($scope) {
    console.log('test');
    $scope.$watch("testInput", function(){
        console.log($scope.testInput);
    });
}

这篇关于设定范围财产NG-init没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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