简体   繁体   English

无法在Angular的$ scope中设置值?

[英]Cannot set a value in $scope in Angular?

I am following the tutorial on Lynda.com for AngularJS essential training. 我正在关注Lynda.com上有关AngularJS基本培训的教程。

Part of my index file looks like: 我的索引文件的一部分看起来像:

<div class="container" ng-controller="AppCtrl">
    <h1>AngulAir</h1>
    <ul class="nav nav-pills">
        <li role="presentation" ng-class="destinationsActive"><a href="#/" ng-click="setActive(destinations)">Destinations</a></li>
        <li role="presentation" ng-class="flightsActive"><a href="#/flights" ng-click="setActive(flights)">Flights</a></li>
        <li role="presentation" ng-class="reservationsActive"><a href="#/reservations" ng-click="setActive(reservations)">Reservations</a></li>
    </ul>
    <div ng-view>
    </div>
    <p>{{ flightsActive }}</p>
</div>

Now when I click on any link it should fire the setActive function defined in the AppCtrl which looks like this: 现在,当我单击任何链接时,应触发AppCtrl中定义的setActive函数,如下所示:

$scope.setActive = function (type) {
    $scope.destinationsActive = '';
    $scope.flightsActive = '';
    $scope.reservationsActive = '';

    $scope[type + 'Active'] = 'active';
};

Now the problem is very simple. 现在问题很简单了。 The function should take the type for example 'destinations' and append 'Active' to it and set the scope variable 'destinationsActive' to active which in turn should be reflected in the ng-class directive of the li tags and the link should be active. 该函数应采用类型“ destinations”,并在其后附加“ Active”,并将范围变量“ destinationsActive”设置为active,这又应反映在li标签的ng-class指令中,并且链接应处于活动状态。

I have tried to insert alert('hello'); 我试图插入alert('hello'); after setting it active which fires up. 将其设置为活动后会触发。 Now this means that the function is indeed being called. 现在,这意味着该函数确实已被调用。 But when I do alert($scope.destinationsActive); 但是当我做alert($scope.destinationsActive); it gives me a blank alert whereas it should give me active as the value. 它给我一个空白的警报,而应该给我积极的价值。

I am not following with the exercise files and I feel that maybe because the tutorial is relatively older, there might be changes in the framework. 我没有遵循练习文件,我觉得也许是因为本教程相对较旧,所以框架可能有所更改。 I have already encountered such problems with the tutorial. 我已经在本教程中遇到了此类问题。 Anyway, what is it that I am doing wrong? 无论如何,我做错了什么?

In your ng-click directives you are passing the argument as a variable, not a string. ng-click指令中,您将参数作为变量而不是字符串传递。

ng-click="setActive(destinations)"

Will pass in the value of the $scope.destinations , which is undefined. 将传入$scope.destinations的值(未定义)。 Try passing in a string ie: 尝试传递一个字符串,即:

ng-click="setActive('destinations')"

Note the single quotes 注意单引号

You need to put parenthesis around the parameters in your html javascript function call. 您需要在html javascript函数调用中的参数周围加上括号。

<a href="#/" ng-click="setActive('destinations')">Destinations</a>

Here is a working example: JSFiddle 这是一个工作示例: JSFiddle

I would recommend you to think of the model binding in more semantic way. 我建议您以更多语义的方式考虑模型绑定。 For example use a checkbox instead of link and set the checkbox value as ng-model to a scope variable instead. 例如,使用复选框而不是链接,并将复选框值ng-model设置为作用域变量。

<input type="checkbox" ng-model="destinations">
<input type="checkbox" ng-model="flights">
<input type="checkbox" ng-model="reservations">

and thereafter use the model to reflect the rest of the changes (this is the answer to the original question, you have to specify a classname and a condition in brackets: 然后使用模型反映其余的更改(这是原始问题的答案,您必须在方括号中指定类名和条件:

<li ng-class="{ destinationsActive: destinations }">

在ng-click中将参数作为字符串传递。

working code : http://jsfiddle.net/Virbhadrasinh/cLsLfkjm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM