Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $scope变量无法解释地未设置_Angularjs - Fatal编程技术网

Angularjs $scope变量无法解释地未设置

Angularjs $scope变量无法解释地未设置,angularjs,Angularjs,我有一个angularjs“child”指令,因此: someAlertMessage: function(){ return { restrict: 'E', template: '<span ng-show="showAlert">whatever</span>', require: '^someInput', controller: function($scope, AcmeService){

我有一个angularjs“child”指令,因此:

someAlertMessage: function(){
    return {
        restrict: 'E',
        template: '<span ng-show="showAlert">whatever</span>',
        require: '^someInput',
        controller: function($scope, AcmeService){
            $scope.setShowAlert = function(aValue){

                AcmeService.getThings(aValue, function(data){ // bog standard http get with callback
                    console.log(data) // THIS LOGS true
                    $scope.showAlert = true; // THIS WORKS
                    $scope.showAlert = data; // THIS DOESNT WORK
                }

            }
        }
        link: function(scope, e, a, c){
            scope.$watch('someinputvalue', function(nv, ov){
                scope.setShowAlert(nv)
            }
        }
    }
}
如第一个清单所述,我有两个案例:

  • 在服务回调中将
    $scope.showAlert
    变量硬编码为true正好达到预期效果:范围会更新,DOM会相应更改以显示范围
  • 将$scope变量设置为从服务返回的值(通过日志记录和调试,我知道是
    true
    )没有任何作用。在这种情况下,如果我将
    $scope.showAlert
    值写入屏幕,它将保持为false
我尝试将服务的返回值硬编码为
true
,而不进行http调用,这也提供了所需的效果

那么,我被难住了,我能做什么?让我知道需要更多的信息


谢谢。

能否尝试将服务调用包装在
$scope.$apply()
块中?可能是由于调用服务的方式,当服务返回结果时,DOM不知道更新。将调用包装到应用程序中是一种快速的测试方法。@ajk感谢您的建议-将调用包装到$scope中。$apply无效-所有情况都像以前一样响应。它记录为true还是“true”?你明白我的意思。。。如果你使用firebug try,我写的一个小扩展。它将帮助您检查与DOM元素关联的$scope对象。建议创建复制问题$scope的实时演示。showarert=data=='true';这就是@KosProv的建议。
<someInput>
    <someAlertMessage></someAlertMessage>
</someInput>
<div>
    <!-- things -->
    <div ng-transclude></div>
</div>