Skip to content Skip to sidebar Skip to footer

Better Way To Listen For Changes - AngularJS

I am just playing around with Angular JS and would appreaciate some inputs from you master guys out there :D What I am doing is implementing my own localization code (I know, I kno

Solution 1:

Edit: in response to comments, here's another version: http://jsfiddle.net/dn3w41cx/9/

It doesn't require data work in the controller at all.

Here's the code:

javascript

function LanguageController($scope) {
    $scope.languages = [
        {
            name:'English',
            'term1': 'This is Term 1 in English',
                'term2': 'This is Term 2 in English'
        },
            {
                name:'French',
            'term1': 'This is Term 1 in French',
                'term2': 'This is Term 2 in French'
        },
             {
                 name:'Vietnamese',
            'term1': 'This is Term 1 in Vietnamese',
                'term2': 'This is Term 2 in Vietnamese'
        }
    ];

}

html

<div ng-app>
    <div ng-controller="LanguageController">
        <div><span>Current Language: {{language.name}}</span>
        </div>
        <div><span>Term 1: {{language.term1}}</span>
        </div>
        <div><span>Term 2: {{language.term2}}</span>
        </div>
        <div>
            <select ng-options="item.name for item in languages" ng-model="language" 
                    ng-init="language=languages[0]">
            </select>
        </div>
    </div>
</div>

Old answers:

If you want nice syntax, then you can implement something like this in the changeLanguage function: http://jsfiddle.net/dn3w41cx/6/

Otherwise, you can do it without any special changes in the javascript like this fiddle: http://jsfiddle.net/dn3w41cx/5/


Post a Comment for "Better Way To Listen For Changes - AngularJS"